Html Angular:如果条件为真,如何更改单元格表的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21964641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:01:13  来源:igfitidea点击:

Angular: How to change the color of cell table if condition is true

htmlangularjshtml-tablebackground-color

提问by Sara

I have an object which has a variable called changeColor. In my html table I want to change the cell color if changeColoris true. I am using angular.

我有一个对象,它有一个名为changeColor. 在我的 html 表中,如果changeColor为真,我想更改单元格颜色。我正在使用角度。

    <tr ng-repeat="list in results">
    <% if (!{{list.changeColor}} ) %>
    <% { %>
        <td bgcolor="red">{{list.value}}</td>
    <% } %>
    <td>{{list.price}}</td>

But after testing it is always red and the <% if (! ) %> <% { %> <% } %>is written in my html page! Can you please help me?

但是经过测试它总是红色并且<% if (! ) %> <% { %> <% } %>写在我的html页面中!你能帮我么?

Tested this

测试了这个

<td style=".red {bgcolor: red;} .black {bgcolor: black;}"
 ng-class='{red : list.changeColor, black: !list.changeColor}'>
 {{list.price}}</td>

But it does not work

但它不起作用

回答by Fizer Khan

You have to use ng-class

你必须使用 ng-class

<tr ng-repeat="list in results">
    <td ng-class='{red : list.changeColor, black: !list.changeColor}'>{{list.value}}</td>
    <td>{{list.price}}</td>
</tr>

CSS

CSS

<style type="text/css">
.red {
    color: red; 
}

.black {
    color: black;
}
</style>

回答by crunch

Don't put so much logic in your views. Try this instead:

不要在你的观点中加入太多逻辑。试试这个:

ngClass directive

ngClass 指令

It will allow you to change the td class based on an expression.

它将允许您根据表达式更改 td 类。

回答by rabyusbeef

 <td ng-class="{'negative':daily.MTDCompSales<0,'positive':daily.MTDCompSales>0}">{{daily.MTDCompSales | number:0}}</td>

This line of code changes based on data value, if data is positive or negative

这行代码根据数据值变化,如果数据是正数或负数