Add multiple classes with ng-class, where one of the classes is conditional

Jukke picture Jukke · Apr 17, 2014 · Viewed 16.7k times · Source

I got this code:

ng-class="{selectedHeader: key == selectedCol}"

It works but I would like too add the 'key' value as a class too, Ive tried:

ng-class="[{selectedHeader: key == selectedCol}, key]"

But that wont work, does anyone know how to solve this?

Answer

Michal Charemza picture Michal Charemza · Apr 17, 2014

If you always want key to be added as a class:

ng-class="{selectedHeader: key == selectedCol, key: true]"

or you can just put it into a class attribute with {{}} interpolation.

ng-class="{selectedHeader: key == selectedCol}" class="{{key}}"

Just for completeness, if you only want to include it if it's equal to selectedCol:

ng-class="{selectedHeader: key == selectedCol, key: key == selectedCol}"