How to render HTML containing Angular Components dynamically at run-time in the other component using bootstarp and Angular 15
Loading
How to render HTML containing Angular Components dynamically at run-time in the other component using bootstarp and Angular 15
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Deepak RawatPosted Jul 4, 2023, 10:21 AM
Import the necessary modules and classes: In your component file, import the following Angular classes:
Set up the view container reference: In your component class, declare a ViewChild of type ViewContainerRef to access the target container where the HTML will be dynamically rendered.
Inject the ComponentFactoryResolver: In the constructor of your component, inject the ComponentFactoryResolver.
Create a method in your component that will handle the dynamic rendering. This method should take an HTML string containing the Angular component markup as a parameter.
Mohamed Azarudeen ZPosted Jul 4, 2023, 10:02 AM
Hi Piyush
To dynamically render HTML containing Angular components at runtime, you can use Angular's
ComponentFactoryResolveralong withViewContainerRef. Here's a step-by-step guide:DynamicComponent.In the component where you want to dynamically load other Angular components, inject
ComponentFactoryResolverandViewContainerRef.Finally, you can call the
loadDynamicComponent()method fromParentComponentwhenever you want to dynamically renderYourDynamicComponent.Hope this helps