To my dismay angular no longer supports the ng-include directive. My code base uses hundreds of shared partial html files. I cant seem to find any documentation. Can anyone point meet in the right direction. Is the best practice just to componentize every html file?
Asked
Active
Viewed 434 times
1 Answers
1
You can use ngTemplateOutlet
directive as an alternative to ng-include
in angular by passing a TemplateRef
. Here I mentioned a sample code to do it.
@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>;
}

Sunny
- 577
- 6
- 20
-
Thats what I was afraid of. Thanks. – blankpage985 Jan 21 '20 at 19:15
-
was it helpful?@blankpage985 – Sunny Jan 21 '20 at 19:17
-
1Maybe Im not explaining in the best way. I probably have 1000 html files that are used more than once. I currently display them using ng-include. I am wondering what the angular team would suggest I do to replace my ng-includes as they are no longer supported. It sounds like the answer is to create a component for each one (I used the word componentize). This is gonna take me a very long time so I just wanted to make sure this was the correct direction to go before I started. Didnt know if I may be missing something. @Sunny – blankpage985 Jan 21 '20 at 19:24