htmlangularjsangularjs-ng-repeatangularjs-ng-class

AngularJS ng-class Expression Not Working Properly?


I am using ng-repeat to list out the number of home runs vs away run in a simple table. I am applying a class based on which team is winning using the following snippet of code:

  <tr ng-class="{winning : game.linescore.r.home > game.linescore.r.away}">
    <td>{{ game.home_team_name }}</td>
    <td>{{ game.linescore.r.home }}</td>
  </tr>

The code works if the number of home runs is greater than the number of away runs but only if this number is less than 10.

Example:

Works As Expected: 9 > 5

Does Not Work: 11 > 5

I am wondering does anyone know how overcome this issue?


Solution

  • If your values are strings, then it makes sense, as '9' > '5' but '11' < '5'. You'll need to convert to integer with e.g. parseInt.