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

泪湿孤枕 提交于 2021-02-06 19:57:47

问题


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

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

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


回答1:


You have to use 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

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

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



回答2:


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

ngClass directive

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




回答3:


 <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



来源:https://stackoverflow.com/questions/21964641/angular-how-to-change-the-color-of-cell-table-if-condition-is-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!