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

194
Visualizações
How to parse a stringified HTML and bind it to the DOM with its attributes (angular)

i have this kind of string

"<div style="text-align: center;"><b style="color: rgb(0, 0, 0); font-family: "Open Sans", Arial, sans-serif; text-align: justify;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sit amet vestibulum eros, ac aliquam magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. In ante urna, dictum sed porta ut, consectetur eu felis. Pellentesque eleifend egestas augue, iaculis ultricies nulla volutpat at. Vivamus sagittis est eu rutrum blandit"

which includes style attributes i want to bind it to my html but whatever i try i always lose some styles like the first one

("<div style="text-align: center;" )

any idea how to put this into my html without losing any styles ? thank you

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

0

Well i do assume you've used [innerHtml] directive on your HTML element like this:

// Your *.html template
<p [innerHTML]="myUnsafeHtmlString"></p>

// Your *.component.ts controller file
myUnsafeHtmlString: string = `<div style="text-align: center;">
<b style="color: rgb(122, 122, 122); font-family: 'Open Sans', Arial, sans-serif; text-align: justify;">Lorem ipsum...</b></div>`;

In that case Angular will block some HTML attributes because of safety reasons. You have info about this inside your browser developer tools -> console tab.

More about DomSanitizer in Angular - https://angular.io/api/platform-browser/DomSanitizer

To achieve "unsafe" html in your templates you need to explicitly call it thru DomSanitizer. In my example I've used @Pipe to achieve this, however your can also use it from your components controller.

// Your *.html template
<p [innerHTML]="myUnsafeHtmlString | safeHtml"></p>

// Your *.pipe.ts
@Pipe({ name: 'safeHtml' })
export class SafeHtmlPipe implements PipeTransform {
  constructor(private sanitized: DomSanitizer) {}
  transform(value) {
    return this.sanitized.bypassSecurityTrustHtml(value);
  }
}

// Your *.component.ts controller file
myUnsafeHtmlString: string = `<div style="text-align: center;">
<b style="color: rgb(122, 122, 122); font-family: 'Open Sans', Arial, sans-serif; text-align: justify;">Lorem ipsum...</b></div>`;

Full stackblitz example:

https://stackblitz.com/edit/angular-ivy-yxcwjb?file=src%2Fapp%2Fapp.component.html

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