I'm tryng to get a DOM Element in Angular, console throws error: core.js:5882 ERROR TypeError: Cannot read property 'nativeElement' of undefined. If i go to another route and then go back i can see the correct element in console, but if i load the page at the first time it throws error, how can i solve this? The element i'm trying to get is nested by a div with *ngIf that evaluates an asynchronous variable. Thanks in advance
@ViewChild('inputSearch') el: ElementRef;
ngAfterViewInit(){
console.log(this.el.nativeElement);
}
<div *ngIf="asynchronous variable">
<input type="text" #inputSearch >
</div>