I'm struggling to produce a style inside an *ngFor
based on the iteration. My template looks like this:
<div
*ngFor="let color of options.colors"
<!--attempt to apply style using color variable-->
>
</div>
Where options is simply an object
with a property color
of type Array<string>
. These are the attempts I have made so far:
style="border-color: {{color}} !important"
style="{{'border-color: ' + color + ' !important'}}"
These both produce the following output in the console:
Angular.io outlines why this occurs here, citing the reason for this as protection against XSS attacks. The page does not break, but the colors are simply not applied; a default color in one of my CSS files is used instead.
I figured I would instead go about a more Angular-y method:
[style.border-color]="{{color}} !important"
[style.border-color]="{{color + ' !important'}}"
[ngStyle]="{ 'border-color': '{{color}} !important' }"
[ngStyle]="{{'{ border-color: ' + color + ' !important }'}}"
EDIT: also
[attr.style]="'border-color: {{color}} !important'"
But all of these attempts produced the following error or something similiar:
(If it makes a significant difference, it was specifically the third of those attempts that threw this error.)
I have a very limited understanding of [style.*characteristic*]
and [ngStyle]
, and I can't seem to find anything online explaining how to use them inside an *ngFor
.
Can anyone point me towards what I'm doing wrong?