angularangular-renderer2

Angular 5 access divs list using @ViewChildren


I want to fetch all divs starting with the id 'div'. To do so, i use @ViewChildren but i'm not able to access the divs because i have an empty array , why ?

My template

<div id="div-1">Div 1</div>
<div id="div-2">Div 2</div>
<input type="button" (click)="getDivs()">

Component

@ViewChildren('div') divs: QueryList<any>;
divList : any[];

getDivs(){ 
   this.divList = this.divs.filter(x => x.id.lastIndexOf('div-', 0) === 0);  
   console.log(this.divList);  
      // this.divList return an empty array but i should have two results  
}

Solution

  • As mentioned in this detailed answer, the valid selectors for ViewChildren include component types, directive types, and template reference variables. You cannot retrieve DOM elements with ViewChildren using CSS selectors like HTML element types (e.g. div) or class names.

    One way to make it work in your case is to generate the div elements with an ngFor loop, and associate a template reference variable #divs to them:

    <div #divs *ngFor="let item of [1,2]" [id]="'div-' + item">Div {{item}}</div>
    <button (click)="getDivs()">Get divs</button>
    

    You can then retrieve them in code with ViewChildren, using the template reference variable:

    @ViewChildren("divs") divs: QueryList<ElementRef>;
    
    getDivs() {
      this.divs.forEach((div: ElementRef) => console.log(div.nativeElement));
    }
    

    See this stackblitz for a demo.