我有两个组件:列表和详细信息
在列表组件中,我想渲染多个细节组件.
list.component.ts
@Component({ selector: 'detail',templateUrl: './detail.component.html' }) export class DetailComponent { @ViewChild('details') details; public items = { // ... } }
list.component.html
<detail *ngFor="let item of items" #details></detail>
注意#details tempalte引用变量.我想访问所有细节组件.可以将#details变量设为数组吗?
@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