Sanitization warnings on color strings in Angular2

2,742 views
Skip to first unread message

Forrester Cole

unread,
Jul 8, 2016, 5:00:35 PM7/8/16
to AngularJS
Hi all,
   I'm trying to set the background-color style of an element using a template. I have template html code that looks like:

<div class="picker-flex-box">
  <div *ngFor="let color of colors"
    class="color-swatch"
    style="background-color: {{color}}"
  </div>
</div>

where colors is a list of color strings in the hex RGB format. Running the app, I get a lot of warnings like:

WARNING: sanitizing unsafe style value background-color: #7C4835 (see http://g.co/ng/security#xss).

is this actually an unsafe string? If so, what should I do differently to set the color of this element? I've tried using the rgb() format and got the same results.

Thanks a lot,

Forrester

Lucas Lacroix

unread,
Jul 8, 2016, 5:07:38 PM7/8/16
to ang...@googlegroups.com
You're better off using Angular2 style:
<div *ngFor="let color of colors"
    class="color-swatch"
    [ngStyle]="{'background-color': color}"
  </div>

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Lucas Lacroix
Computer Scientist
System Technology Division, MEDITECH

Forrester Cole

unread,
Jul 8, 2016, 5:16:44 PM7/8/16
to AngularJS
Fixed it, thanks!
Reply all
Reply to author
Forward
0 new messages