簡體   English   中英

帶有* ngFor的Angular 2模板引用變量

[英]Angular 2 template reference variable with *ngFor

我有兩個組件:列表和詳細信息

列表組件中,我想渲染多個細節組件

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:獲取模板中的元素

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM