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

147
Vistas
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 Respuestas
Responde la pregunta

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 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