0

I'm trying to figure out how to do an if statement so that if the player's batting average is more than .250 it will add a class to the tr of success.

I found the following stack question but I"m not sure which way of utilizing these I could use or should use and how.

http://stackoverflow.com/questions/15810278/if-else-statement-in-angularjs-templates


<tbody>
    <tr ng-repeat="player in startingLineupArray track by $index" {{ player.playerBattingAverage > '.250' ? 'class="success"' : ' ' }}>
        <td>{{ $index }}</td>
        <td>{{ player.playerName }}</td>
        <td>{{ player.playerPosition }}</td>
        <td>{{ player.playerBattingAverage }}</td>
        <td>{{ player.playerBats }}</td>
    </tr>
</tbody>
user3732216
  • 1,579
  • 8
  • 29
  • 54

2 Answers2

1

if you want to dynamically add a class, you should use ngClass instead.

AngularJS ngClass conditional

Community
  • 1
  • 1
huan feng
  • 7,307
  • 2
  • 32
  • 56
1

Here is how you can do it...

<tbody> 
  <tr ng-repeat="player in startingLineupArray track by $index" ng-class="{success: player.playerBattingAverage > .250}">
    <td>{{ $index }}</td> 
    <td>{{ player.playerName }}</td>
    <td>{{ player.playerPosition }}</td>
    <td>{{ player.playerBattingAverage }}</td> 
    <td>{{ player.playerBats }}</td>
  </tr>
</tbody>
Srinivas Paila
  • 817
  • 6
  • 10
  • Is it possible to do two ng-classes in one tag in the situation where if they are batting more than .250 its success class (green) but if its lower than .200 then its an error class (red)? – user3732216 Nov 14 '14 at 03:51
  • 1
    Write like this, ng-class="{success: player.playerBattingAverage > .250, error: player.playerBattingAverage < .200}" – Manish Kr. Shukla Nov 14 '14 at 04:27