1

I'm trying to breakdown bigger HTML chunks which has repeated markup based on flags.

Is there a way we can make html code split into files one.html, two.html and render them into my component (.ts will decide which html to load )

Basically idea is one component -> multiple html files

I can have single big html like previous and use ng-template/ngContentOutlet but this is still making readability a issue for a 2k+ lines on html.

we're on angular 18.x standalone, any suggestions are golden

2
  • 1
    It's not possible, you can only have one HTML file per component. You should split your huge component into multiple smaller components instead. Commented Oct 23, 2024 at 14:10
  • @JSONDerulo earlier we used to have ngInclude which takes path but for performance reasons its depreciated and looks like its a dead end but really good to have one hopefully in future. Commented Oct 23, 2024 at 14:18

1 Answer 1

2

AngularJS ng-include is not exactly but replaced by ng-template/ngContentOutlet of Angular.


You can create a component that is specifically designed to hold reusable ng-templates.

The component will contain all the templates in it's HTML and ViewChild for each of the templates.

reusable.com.ts

@Component({ ... })
export class ReusableTemplatesComponent {
    @ViewChild('test') test!: TemplateRef<any>;
}

reusable.com.html

<ng-template #test let-str="str">
    {{str}} World!
</ng-template>

Then when you want to use these templates, add the component to the bottom and create a reference using template reference, access the template and pass the context.

<ng-container *ngTemplateOutlet="reusableTemplates.yesNoFlagTemplate; 
    context:{str: 'Hello'}"></ng-container>
....
<app-reusable-templates #reusableTemplates/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.