sanjihan
sanjihan

Reputation: 5992

nativeElement of ViewChildren's first is undefined

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?

Upvotes: 3

Views: 2009

Answers (1)

yurzui
yurzui

Reputation: 214007

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

Upvotes: 7

Related Questions