我有两个组件:列表和详细信息
在列表组件中,我想渲染多个细节组件.
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