Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

417
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!