在Angular中动态设置样式 [英] Set style dynamically in Angular

查看:1283
本文介绍了在Angular中动态设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下标记

<tr *ngFor='let activity of pagedWorkflowActivities' style="background-color:{{activity.status == 'Pending' ? 'red' : 'green'}}">
.
.
.
.
</tr>

如前所述,如果activity.status字段待处理,则将背景色设置为红色,否则将设置为绿色.但这是行不通的.检查后,我发现它呈现为

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">

推荐答案

[style.background-color]="activity.status == 'Pending' ? 'red' : 'green'"

[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

这篇关于在Angular中动态设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆