Access to ViewChildren list after NgFor

Viewed 2059

I woul like to access all the viewChildren after one ng for to a component, for example:

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

Now, i have a ViewChildren like this:

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

but when i console.log him, it only appears one and i would like to have the reference of every of the ngFor

2 Answers

try this .toArray, it works :

@ViewChildren('item') item;

this.item.toArray()

You can subscribe to changes to get notified when the queried elements change:

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