Css 根据角度6中的值更改td中文本的颜色
我的表中有一个状态列 我需要根据值显示不同颜色的文本。我正在使用Angular 6Css 根据角度6中的值更改td中文本的颜色,css,angular6,Css,Angular6,我的表中有一个状态列 我需要根据值显示不同颜色的文本。我正在使用Angular 6 <table> <th>Status</th> <th>Name</th> <tr *ngFor="let data of data > <td>{{data.status}}</td> <td>{{data.name}}</td> </tr> </table> 地位
<table>
<th>Status</th>
<th>Name</th>
<tr *ngFor="let data of data >
<td>{{data.status}}</td>
<td>{{data.name}}</td>
</tr>
</table>
地位
名称
首先,可以实例化对应颜色的数组:
打字稿:
colors = [{ status: "Passed", color: "red" }, { status: "Approuved", color: "red" },
{ status: "warning", color: "green" }, { status: "Ignored", color: "yellow" }]
然后可以使用ngStyle动态设置样式:
HTML
...
<tr *ngFor="let row of data" [ngStyle]="{'background':getColor(row.status)}">
...
</tr>
它根据相应颜色数组中的状态返回颜色值
就这样
。谢谢。这就是我要找的@VegaVery很好,我将其更改为返回此.colors.filter(item=>item.status==status)[0]?.color??'黑色“
,非常适合我的用例:)
getTheColor(status) {
return this.colors.filter(item => item.status === status)[0].color
// could be better written, but you get the idea
}