令我沮丧的是,angular不再支持ng-include指令。我的代码库使用数百个共享的部分html文件。我似乎找不到任何文档。任何人都可以在正确的方向见面。最佳做法是仅对每个html文件进行组件化吗?
您可以通过传递ngTemplateOutlet
来使用ng-include
指令替代TemplateRef
的角度。
@Component({
selector: 'child',
template: `
<div>
child template which includes myTemplate
<ng-container [ngTemplateOutlet]="myTemplate"></ng-container>
</div>`
})
export class ChildComponent {
@Input() myTemplate: TemplateRef<any>;
}
@Component({
selector: 'app-root',
template: `
<p>Parent</p>
<child [myTemplate]="myTemplate"></child>
<ng-template #myTemplate>hi julia template!</ng-template>
`
})
export class AppComponent {
@ViewChild('myTemplate', {read: TemplateRef}) myTemplate: TemplateRef<any>;
}