Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 根据角度6中的值更改td中文本的颜色_Css_Angular6 - Fatal编程技术网

Css 根据角度6中的值更改td中文本的颜色

Css 根据角度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> 地位

我的表中有一个状态列 我需要根据值显示不同颜色的文本。我正在使用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>

地位
名称

首先,可以实例化对应颜色的数组:

打字稿:

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
}