Search code examples
cssangularjshtml-table

Change of table cell text color based on condition using ng-if and ng-style


I want to display the text in a table cell with alternate color combination depending on some condition as follows.

<tr ng-repeat="x in data">
<td>
<span ng-if="((x.time2 < x.time1) ||  (x.time2 < x.time3))" ng-style="color:red" {{x.val}}></span>
<span ng-if="((x.time2 === x.time1) &&  (x.time2 === x.time3)" ng-style="color:blue" {{x.val}}></span>
</td>
</tr>

But i am not getting the value displayed in red color though the x.time2 is less than x.time1. Sample time values are time1="2017-03-15 06:06:56", time2="2017-03-08 07:09:40" and time3="2017-03-08 07:09:40". All time values are in string format.


Solution

  • Try to change your code in this way:

        <table>
            <tbody>
                <tr ng-repeat="x in data">
                <td>
                <span ng-if="x.time2 < x.time1 || x.time2 < x.time3" ng-style="{color: 'red'}" >{{x.val}}</span>
                <span ng-if="x.time2 === x.time1 && x.time2 === x.time3" ng-style="{color: 'blue'}" >{{x.val}}</span>
                </td>
                </tr>
            </tbody>
        </table>
    

    I also moved x.val inside span... I don't know if it was an error or not.