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

416
Vistas
How to use bypassSecurityTrustStyle correctly

I am trying to work with some simple CSS and JavaScript examples in an Angular way.

The plain JavaScript way to change the background color on an element would look something like this:

this.container.style.backgroundColor = color;

The color is created randomly in JavaScript. The template could look like this:

<div id="container"
  (click)="changeColor($event)"
  [style]="{'color': color}">
</div>

However, this will give the following error:

WARNING: sanitizing unsafe style value [object Object] (see http://g.co/ng/security#xss).

Using this function does not work:

this.color = this.sanitizer.bypassSecurityTrustStyle(this.color);

Creating a function to do it also does not work using this style [style.color]="transform(color)"

transform(value) {
   return this.sanitizer.bypassSecurityTrustStyle(value);
}

I have created a plunker to demonstrate this problem.

The correct behaviour using plain JavaScript is shown in the first example on this page

What is the correct way to do this? Any help would be much appreciated.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I think you need to use [styles] or [ngStyle] like this :-

<some-element [ngStyle]="{'font-style': styleExp}">...</some-element>

or

<some-element [styles]="myObj}">...</some-element>

Where in the templates component myObj is myObj = {color: 'red', font-size: '1.1em'}

Docs for [ngStyle] here

about 4 years ago · Santiago Trujillo Denunciar

0

The example plunker was using bypassSecurityTrustStyle correctly:

 this.color = this.sanitizer.bypassSecurityTrustStyle(this.color);

However, the template needed to set the value like this:

[style.background]="color"
about 4 years ago · Santiago Trujillo 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