【问题标题】:Access to ViewChildren list after NgForNgFor 后访问 ViewChildren 列表
【发布时间】:2018-05-07 19:01:25
【问题描述】:

我想在一个组件之后访问所有的viewChildren,例如:

<component #item *ngFor="let item of items" [parameter1]="item.parameter1" [parameter2]="item.parameter2"></component>

现在,我有一个像这样的 ViewChildren:

@ViewChildren('item') item: QueryList<Item>

但是当我 console.log 他时,它只出现一个,我希望有每个 ngFor 的参考

【问题讨论】:

    标签: javascript angular components


    【解决方案1】:

    您可以订阅更改以在查询的元素更改时收到通知:

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

    【讨论】:

      【解决方案2】:

      试试这个 .toArray,它可以工作:

      @ViewChildren('item') item;
      
      this.item.toArray()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-21
        • 2020-07-20
        • 1970-01-01
        • 1970-01-01
        • 2020-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多