I would like to access an element with ViewChild that is rendered with innerHtml, but in the lifecycle hook AfterViewInit, logging the element return undefined.
Below is the code used:
import { Component, ViewChild, ChangeDetectorRef, AfterViewInit } from '@angular/core';
@Component({
template: `<p [innerHTML]="'<ng-template #innerHtmlNgTemplate></ng-template>' | safeHtml"></p>`
})
export class AppComponent {
@ViewChild('innerHtmlNgTemplate') public innerHtmlNgTemplate;
ngAfterViewInit() {
console.log(this.innerHtmlNgTemplate); // undefined
}
}
The question is how can I access the <ng-template></ng-template>
with ViewChild or in another way?
Cheers
EDIT:
Here is a stackblitz showing the issue. https://stackblitz.com/edit/angular-k1xeh6?file=src%2Fapp%2Fapp.component.ts
EDIT2:
In the stackblitz I sanitized the html, if this isn't done the ids are removed.