What is the alternative for “ng-include” in angular2?

前端 未结 3 1875
遥遥无期
遥遥无期 2021-02-19 21:40

Is there any alternate way in angular to achieve what ng-include does in angularjs?

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 22:29

    The closest to ng-include is ngTemplateOutlet directive. You need to pass a TemplateRef to it and optional context. Something like that:

    @Component({
      selector: 'child',
      template: `
        
    here is child template that includes myTemplate
    ` }) export class ChildComponent { @Input() myTemplate: TemplateRef; } @Component({ selector: 'app-root', template: `

    Parent

    hi julia template! ` }) export class AppComponent { @ViewChild('myTemplate', {read: TemplateRef}) myTemplate: TemplateRef; }
    1. Parent component querys the template and passes it to the child component
    2. Child component uses ngTemplateOutlet directive to create view and render it.

提交回复
热议问题