Lets say you have an array that is rendered in a ul
with an li
for each element and a property on the controller called selectedIndex
.
ng-class supports an expression that must evaluate to either
So, using form 3) we can simply write
ng-class="{'selected': $index==selectedIndex}"
See also How do I conditionally apply CSS styles in AngularJS? for a broader answer.
Update: Angular 1.1.5 has added support for a ternary operator, so if that construct is more familiar to you:
ng-class="($index==selectedIndex) ? 'selected' : ''"