In my html i have async pipe and ElementReference. In ts iam setting data comes with async pipe to nativeElement.innerHTML but iam getting this on console.
Cannot read properties of undefined (reading 'nativeElement')
Html
<div *ngIf="(article$ | async) as article">
<div #articleBody>
</div>
</div>
Ts
@ViewChild("articleBody") articleBody: ElementRef;
ngAfterViewInit() {
this.article$ = this.articleService.article$.pipe(
tap(x => {
this.articleBody.nativeElement.innerHTML = x.body
})
)
}
How can i solve this ?
You need to handle this with run change detector manually. Angular will init
ViewChildonngAfterInitbut you init the data in it, too. So try thisThis force Angular to check all changes. Then your ViewChild is available.