Попробуйте это, если вы хотите иметь ссылку на ng-template
import { Component, ViewChild, ChangeDetectorRef, AfterViewInit, ElementRef } from '@angular/core';
@Component({
selector: 'app-component',
template: `<p
[innerHTML]="'This is the innerhtml, and the following tag I want to select with ViewChild: '" >
<ng-template #innerHtmlNgTemplate></ng-template>
</p>
`
})
export class AppComponent {
@ViewChild('innerHtmlNgTemplate', {static: true}) public innerHtmlNgTemplate;
ngAfterViewInit() {
console.log(this.innerHtmlNgTemplate); // find value
}
}
И используйте это, если вы хотите сослаться на внутренний HTML без изменения шаблона
import { Component, ViewChild, ChangeDetectorRef, AfterViewInit, ElementRef } from '@angular/core';
@Component({
selector: 'app-component',
template: `<p #innerHtmlNgTemplate
[innerHTML]="'This is the innerhtml, and the following tag I want to select with ViewChild: <ng-template ></ng-template>'"></p>`
})
export class AppComponent {
@ViewChild('innerHtmlNgTemplate', {static: true}) public innerHtmlNgTemplate;
ngAfterViewInit() {
console.log(this.innerHtmlNgTemplate.nativeElement.innerHTML); // prints innerhtml
}
}