我有两个组件:列表和详细信息
在列表组件中,我想渲染多个细节组件.
list.component.ts
@Component({
selector: 'detail',
templateUrl: './detail.component.html'
})
export class DetailComponent {
@ViewChild('details') details;
public items = {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
list.component.html
<detail *ngFor="let item of items" #details></detail>
Run Code Online (Sandbox Code Playgroud)
注意#details tempalte引用变量.我想访问所有细节组件.可以使#details变量成为一个数组吗?
@ViewChildren('details') details:QueryList<DetailComponent>;
ngAfterViewInit() {
console.log(this.details.toArray());
this.details.changes.subscribe(changes => {
console.log(this.details.toArray());
});
}
Run Code Online (Sandbox Code Playgroud)
另请参见angular 2/typescript:获取模板中的元素
| 归档时间: |
|
| 查看次数: |
6253 次 |
| 最近记录: |