Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
Angular 13 load/compile dynamic Angular component from HTML string

I want to load or compile Angular elements as HTML from string into my component.

I am getting HTML string from API as a string and I want to load and render all HTML structural directives from that string.

 let str = `<div>Testing here
  <div *ngFor="let i of [1,2,3,4]">{{i}}</div>
 </div>`;

setTimeout(() => {
  const componentRef: ComponentRef<DynamicComponent> =
    this.createDynamicComponent<DynamicComponent>(
      DynamicComponent,
      this.vc
    );
  componentRef.instance.html = this.sanitizer.bypassSecurityTrustHtml(str);
});

But it output as instead of ngFor loop it simply loads as string.

Load Dynamic HTML
Testing here
{{i}}

Here is sample I tried

Here above is just sample HTML string. It can be anything from server like ngIf or any other inbuilt Angular directives

I have also tried using custom element createCustomElement but in that also I was not able to render Angular directives runtime from string.

Edit 1

Similar thing is working fine for This stackblitz example but If I download and run locally same error.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I downloaded the example at stackblitz and got it working. You can see the complete code here - https://github.com/phalgunv/Angular-Dynamic-Component-From-String

It required a couple of fixes needed to get it working for prod build.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda