带有* ngFor的Angular 2模板引用变量

前端之家收集整理的这篇文章主要介绍了带有* ngFor的Angular 2模板引用变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个组件:列表和详细信息

在列表组件中,我想渲染多个细节组件.

list.component.ts

@H_301_5@@Component({ selector: 'detail',templateUrl: './detail.component.html' }) export class DetailComponent { @ViewChild('details') details; public items = { // ... } }

list.component.html

@H_301_5@<detail *ngFor="let item of items" #details></detail>

注意#details tempalte引用变量.我想访问所有细节组件.可以将#details变量设为数组吗?

@H_301_5@@ViewChildren('details') details:QueryList<DetailComponent>; ngAfterViewInit() { console.log(this.details.toArray()); this.details.changes.subscribe(changes => { console.log(this.details.toArray()); }); }

另见angular 2 / typescript : get hold of an element in the template

猜你在找的Angularjs相关文章