Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda