Reputation: 503
How do we change the color dynamically based on a value in angular typescript template?
For example if item.status == 'Active' && ang item.role == 'Admin'
set color to color:#00B0D
else set color to #707070
.
<mat-icon aria-hidden="false" aria-label="Example home icon" style="color:#00B0DB">
check_circle
</mat-icon>
Upvotes: 0
Views: 2381
Reputation: 21
Using ngStyle
you can set specific styles based on some condition.
Example:
<mat-icon
[ngStyle]="{'color': item.status === 'Active' && item.role === 'Admin' ? '#00B0DB' : '#707070'}"
>check_circle</mat-icon>
Upvotes: 1
Reputation: 1202
Use NgStyle directive to apply conditional inline styles.
Example:
<mat-icon
aria-hidden="false"
aria-label="Example home icon"
[ngStyle]="{'color': item.status === 'Active' && item.role === 'Admin' ? '#00B0DB' : '#707070'}"
>check_circle</mat-icon>
Similarly, you can use NgClass directive to apply CSS classes conditionally.
Upvotes: 3