How would you insert an embedded view from a prepared TemplateRef?

Technology CommunityCategory: AngularHow would you insert an embedded view from a prepared TemplateRef?
VietMX Staff asked 3 years ago

You can create an embedded view using createEmbeddedView method then attach that view to the DOM via TemplateRef:

@Component({
    selector: 'app-root',
    template: `
        <ng-template #template let-name='fromContext'><div>{{name}}</ng-template>
    `
})
export class AppComponent implements AfterViewChecked {
    @ViewChild('template', { read: TemplateRef }) _template: TemplateRef<any>;
    constructor() { }

    ngAfterViewChecked() {
        this.vc.createEmbeddedView(this._template, {fromContext: 'John'});
    }
}