Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can you use Angular's structural directives in innerHtml

I am trying to insert HTML code containing *ngIf and *ngFor directives via a DOM element’s innerHtml property. Is this possible?

I don’t understand how Angular renders things, so I don’t know how to go about doing this. I tried using Renderer2, but this did not help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is not possible as angular template need to be compiled and innerHTML passes directly the string to the dom element.

The closest you can achieve but this is definitely not recommended : Create a component dynamically and pass your string as template.

This is not recommended as this requires to pull the compiler into the production bundle, which is heavy and also JIT components are less efficient.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading