简体   繁体   English

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

[英]Angular 2 template reference variable with *ngFor

I have two components: list and detail 我有两个组件:列表和详细信息

In list component I want to render multiple detail components . 列表组件中,我想渲染多个细节组件

list.component.ts list.component.ts

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

    public items = {
        // ...
    }
}

list.component.html list.component.html

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

Notice the #details tempalte reference variable. 注意#details tempalte引用变量。 I want to access all of the detail components. 我想访问所有细节组件。 It is possible to make the #details variable an array ? 可以使#details变量成为一个数组吗?

@ViewChildren('details') details:QueryList<DetailComponent>;

ngAfterViewInit() {
  console.log(this.details.toArray());
  this.details.changes.subscribe(changes => {
    console.log(this.details.toArray());
  });
}

See also angular 2 / typescript : get hold of an element in the template 另请参见angular 2 / typescript:获取模板中的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM