Set style dynamically in Angular
I have following markup
<tr *ngFor='let activity of pagedWorkflowActivities' style="background-color:{{activity.status == 'Pending' ? 'red' : 'green'}}">
.
.
.
.
</tr>
As it says, if activity.status
field is pending then make background color red otherwise green. But it doesn't work. After inspecting I found it renders it like
<tr ng-reflect-style="unsafe">
Solution 1:
[style.background-color]="activity.status == 'Pending' ? 'red' : 'green'"
or
[ngStyle]="{'backgroundColor': activity.status == 'Pending' ? 'red' : 'green' }"
For your rendering result see also In RC.1 some styles can't be added using binding syntax
Solution 2:
Try this one:
[ngStyle]="{'border': user?.keyResults.percentage > 50 ? '3px solid green' : '3px solid red' }"
Solution 3:
bind-
prefix alternative can be used also as below
bind-style.background-color="activity.status == 'Pending' ? 'red' : 'green'"