0

I have a table where rows are generated using AngularJS ngRpeat:

<tr ng-repeat="player in division">
  <td>{{player.name}}</td>
  <td>{{player.score}}</td>
</tr>

The array looks a bit like this:

$scope.divison = [
  {
    name: "John Doe",
    score: "10",
    goingUp : true
  },
  {
    name: "Bob Marley",
    score: "20"
  }
];

Now, what if I wanted to apply ng-class to the table row based on that particular ng-repeat? I would have though this might work:

<tr ng-repeat="player in division" ng-class="'goingUp' : player.goingUp">
  <td>{{player.name}}</td>
  <td>{{player.score}}</td>
</tr>

Alas this doesn't work. Probably because the ng-class doesn't sit inside that repeat element. How can I get this working though?

CaribouCode
  • 13,998
  • 28
  • 102
  • 174

2 Answers2

1

Correct syntax is

ng-class="{'goingUp' : player.goingUp}"
Second2None
  • 462
  • 2
  • 8
  • 22
0

It should be able to do so like in this case. You could write a function in to your controllers scope that returns appropriate class:

$scope.isGoingUp = function(player)
{
  if (player.goingUp) return "goingUp";
  return "notGoingUp";
}

and then call it like this

ng-class="isGoingUp(player)"
Community
  • 1
  • 1
Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66