Search code examples
angularjsclassangularjs-ng-repeatexpressionng-class

Using ng-class with property/value pair expression


I want to be able to use ng-class to trigger a class based on the true/false value of the expression. I know how to do this if it just checking for a property's existence, but what I want to do is input an expression to say 'the value of this property is this'.

So, if I was trying to link a colour to a football player's position, for example, I would like to say:

<p ng-class="{'green': player.position: gk}">

This doesn't work though.

Is there a way of doing this? If so, where am I going wrong?

JSFiddle example: http://jsfiddle.net/Paulos3000/U3pVM/22917/

In this example I want any player who has a position of "gk" to be green.

Thanks in advance


Solution

  • <p ng-class="{'green': player.position == 'gk'}">
    

    This should work :)