Hely Saul Oberto
Hely Saul Oberto

Reputation: 597

Access to ViewChildren list after NgFor

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

Upvotes: 3

Views: 2739

Answers (2)

Fateh Mohamed
Fateh Mohamed

Reputation: 21387

try this .toArray, it works :

@ViewChildren('item') item;

this.item.toArray()

Upvotes: 2

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657957

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());
  });
}

Upvotes: 1

Related Questions