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

148
Visualizações
How to parse a html string with structural directive in angular

I am trying to parse a html string with structural directive inside ng-template. But it is displayed as a string. My exceptions was the structural directive will iterate three times and it will show three li. How can I achieve that?

Here is my code

import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  data = `<div><li *ngFor="let d of [1,2,3]">{{d}}</li> </div>`;
}
<section>
  <ng-container
    [ngTemplateOutlet]="template"
    [ngTemplateOutletContext]="{ html: data }"
  ></ng-container>
</section>

<ng-template #template let-html="html">
  {{ html }}
</ng-template>

Sample code here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Would suggest you perform the interpolation on the controller itself, I don't think interpolation works on string insertion.

import { Component, OnInit, Sanitizer, VERSION } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  data: any = `<div>${[1, 2, 3].map((x) => `<li>${x}</li>`).join('')}</div>`;

  constructor(private sanitizer: DomSanitizer) {}

  ngOnInit() {
    this.data = this.sanitizer.bypassSecurityTrustHtml(this.data);
  }
}

html

<section>
  <ng-container
    [ngTemplateOutlet]="template"
    [ngTemplateOutletContext]="{ html: data }"
  ></ng-container>
</section>

<ng-template #template let-html="html" >
  <div [innerHTML]="html"></div>
</ng-template>

stackblitz

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