I have a HTML page with a table that is created using Angular ng-repeat
and a controller that fetches JSON data.
Here is the simplified version of my table:
product price
------- -----
ABC $1.33
EDF $2.00
And here is the angularJS code inside my HTML page:
<table class="table" ng-controller="someCtrl">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="p in products">
<td>{{p.productName}}</td>
<td>{{p.price}}</td>
</tr>
</tbody>
</table>
Is it possible to change the text color of p.price based on its value? for example, I would like to modify the price value with the color red if p.price > 1.5
I have tried the following answer, but it only works when I hard-coded the number instead of dynamic generating number using ng-repeat
: Can I make a table cell have a different background color if the value =< a particular number with jquery or PHP?
Instead of jQuery, you can achieve this by ng-class
.
<td><span ng-class="p.price > 1.5 ? 'price-red' : ''">{{p.price}}</span></td>
CSS
.price-red {
// apply your code like
color: red;
}
A cleaner way to do it would be using the ng-class directive in angularjs. For the tag, you can optionally give a css class depending on the value of an expression as follows:
<td ng-class="{ 'css-cls-for-color1' : p.price > 3000, 'css-cls-for-color2' : p.price <= 3000}"> </td>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.