admin管理员组文章数量:1023050
So I have a simple ng-repeat
and what I want to do is, if the p.product_quantity < 10
, the background color will be red. Here is my code:
<tr ng-repeat="p in products" style="background-color: red">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
As you can see with that code, all of the rows will be red. How can I perform an if inside the <tr>
? Any help would be much appreciated.
So I have a simple ng-repeat
and what I want to do is, if the p.product_quantity < 10
, the background color will be red. Here is my code:
<tr ng-repeat="p in products" style="background-color: red">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
As you can see with that code, all of the rows will be red. How can I perform an if inside the <tr>
? Any help would be much appreciated.
4 Answers
Reset to default 3You can create a CSS class and use ngClass
directive.
CSS
.red {background-color: red}
HTML
<tr ng-repeat="p in products" ng-class="{'red' : p.product_quantity < 10}">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
Also there is ng-style
directive:
<tr ng-repeat="p in products" ng-style="{'background-color': p.product_quantity < 10 ? 'red' : 'white'}">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
You can use ng-class for that.
<tr ng-repeat="p in products" ng-class="{'red-bg': p.product_quantity < 10}">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
You can achieve this by using ng-class
By using ng-class your template
<tr ng-repeat="p in products" ng-class="bg-read: p.product_quantity < 10">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
your css
.bg-red {
background-color: red
}
You can find the documentation of ng-class here and samples here
So I have a simple ng-repeat
and what I want to do is, if the p.product_quantity < 10
, the background color will be red. Here is my code:
<tr ng-repeat="p in products" style="background-color: red">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
As you can see with that code, all of the rows will be red. How can I perform an if inside the <tr>
? Any help would be much appreciated.
So I have a simple ng-repeat
and what I want to do is, if the p.product_quantity < 10
, the background color will be red. Here is my code:
<tr ng-repeat="p in products" style="background-color: red">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
As you can see with that code, all of the rows will be red. How can I perform an if inside the <tr>
? Any help would be much appreciated.
4 Answers
Reset to default 3You can create a CSS class and use ngClass
directive.
CSS
.red {background-color: red}
HTML
<tr ng-repeat="p in products" ng-class="{'red' : p.product_quantity < 10}">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
Also there is ng-style
directive:
<tr ng-repeat="p in products" ng-style="{'background-color': p.product_quantity < 10 ? 'red' : 'white'}">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
You can use ng-class for that.
<tr ng-repeat="p in products" ng-class="{'red-bg': p.product_quantity < 10}">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
You can achieve this by using ng-class
By using ng-class your template
<tr ng-repeat="p in products" ng-class="bg-read: p.product_quantity < 10">
<td>{{p.product_code}}</td>
<td>{{p.product_name}}</td>
<td>{{p.product_quantity}}</td>
</tr>
your css
.bg-red {
background-color: red
}
You can find the documentation of ng-class here and samples here
本文标签: javascriptHow to change color of table row with if else statement in AngularStack Overflow
版权声明:本文标题:javascript - How to change color of table row with if else statement in Angular? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745560787a2156150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论