Ron
Ron

Reputation: 593

angular 4 how to access ViewChildren _results

i have a list of checkboxes inside an ngFor:

    <md-checkbox
      #hangcheck
      [id]="hangout?.$key"
      class="mychecks"
      >
    I'm Interested
  </md-checkbox> 

i refrence them in the component like so:

@ViewChildren("hangcheck") hangchecks: QueryList<any>;

then in ngAfterViewInit i need to loop them:

  ngAfterViewInit(){
console.log('the array: ',this.hangchecks)
this.hangchecks._results.forEach((item) => {
  console.log('the item: ',item)
});
 }

but i get: Property '_results' is private and only accessible within class 'QueryList' in the console i see this: enter image description here so as you can see there is the array in the _results. but how can i access it and loop it?

Upvotes: 16

Views: 9245

Answers (3)

Sushant Patewar
Sushant Patewar

Reputation: 194

To access ith element in _results in the queryList, call toArray() method:

this.queryListObj.toArray().[i]

Upvotes: 0

Mert Aksoy
Mert Aksoy

Reputation: 412

To access elements, you have to wait until its ready

this.hangchecks.changes.subscribe(a => a.forEach((b, i) => console.log(b)));

Upvotes: 4

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

Reputation: 657957

Call the toArray() method:

this.hangchecks.toArray().forEach((item) => {

Upvotes: 26

Related Questions