-1

I am getting categoryColor from API Responce and I had set border-left: 3px solid {{element.categoryColor}} in inline style. It's working fine and no any other issues in development but in Visual Studio appear red in file name as show as below image.

So I want to proper syntax or best practice to apply color in structure using inline style.

enter image description here

4

2 回答 2

2

Angular have attribute binding which should be used in this case.

If I were you i would move everything from style to some css class then in this span element I would use [style.borderColor]="element.categoryColor"
like so:

.some-class {
    border-left: 3px solid transparent;
    /* ... */
}
<span class="some-class" [style.borderColor]="element.categoryColor"></span>

but you can use it like so as well:

<span style="border-left: 3px solid transparent;" [style.borderColor]="element.categoryColor"></span>
于 2021-08-11T11:37:14.127 回答
0

The best way would be using [ngStyle].

<span [ngStyle]="{ 'border-color': getBorderColor() }">Your content</span>

And for bordercolor, you could use a function which gets the color code from the api response and that function should return the css syntax, like 2px solid purple.

于 2021-08-11T17:09:30.103 回答