Zanshin13 Zanshin13 - 5 days ago 5
Javascript Question

Set <tr> color on change of angular array

I am using angularJS. I've got this code in my view layout:

<table>
<tr ng-repeat="car in cars">
<td>{{ car.name }}</td>
<td>{{ car.speed }}</td>
<td>{{ car.length }}</td>
</tr>
</table>


At some point my angular-controller changes one of
$scope.cars
object.

The point is: I want to change a color of
<tr>
in which changed
car
stays when that happens.

The question is: How can I do it?

Answer

You can flag it as changed.

Set car.changed to true when you've changed it

$scope.car[0].changed = true;

And conditionally apply the changedClass with a background-color.

<table>
    <tr ng-repeat="car in cars" ng-class="{'changedClass': car.changed}">
        <td>{{ car.name }}</td>
        <td>{{ car.speed }}</td>
        <td>{{ car.length }}</td>
    </tr>
</table>
Comments