假设我有以下角度表
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.name}}
</td>
<td>
{{company.price}}
</td>
<td>
{{company.profit}}
</td>
</tr>
</table>
如何使表格单元格的背景颜色取决于单元格的值,这是一个很好的解决方案?可以说,如果公司利润为正,请使其绿色。
答案 0 :(得分:2)
在CSS
td.highlight {
background-color: green;
}
在html中:-
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.price}}
</td>
<td [ngClass]="{'highlight': company.profit > 0}">
{{company.profit}}
</td>
</tr>
</table>
答案 1 :(得分:1)
查看ngStyle
和ngClass
。最简单的用例是直接绑定到style
属性。
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.name}}
</td>
<td>
{{company.price}}
</td>
<td [style.background-color]="company?.profit > 5 ? 'green' : 'red'">
{{company.profit}}
</td>
</tr>
</table>
答案 2 :(得分:1)
您可以将其添加到html中:
<td [style.positive]="company.profit > 0">
并将其发送到您的CSS:
td.positive{
background-color: green;
}