3

I am trying to access ViewChildren's native elements => HTMLElements, but I keep getting undefined response when I do so. Take a look:

list.html

..
<app-element #someElement *ngFor="let element of elements"  [someinput]="someinput"></app-element>
..

list.ts

  @ViewChildren("someElement") someElements:QueryList<ElementRef>;

  @HostListener("window:resize", ["$event"]) 
  resize(event){
    console.log(event);
    console.log((this.someElements.first)); //returns ElementComponent as expected
    console.log((this.someElements.first.nativeElement)); // nativeElement appears to be undefined

  }

I've used ViewChildren numerous times. However, this time it does not work as expected. Is it because ViewChild is an angular component? Am I missing something obvious?

sanjihan
  • 5,592
  • 11
  • 54
  • 119

1 Answers1

5

I would specify what I want to get

@ViewChildren("someElement", { read: ElementRef }) someElements:QueryList<ElementRef>;
                             ^^^^^^^^^^^^^^^^^^^^^
                       please give me ElementRef instead of component instance

See also

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • There you can find link to one more great answer) I suggest you reading it in your free time – yurzui Apr 19 '18 at 09:34