The AngularJS Noob Handbook has some code which reduces class manipulation to a simple expression and binding :
...&
you've probably also seen something like this:
Very rad syntax.
EDIT:
What happens here, is that the " ` Addendum Also, I just realized that you can use a variety of different keys to map to your condition. For example: The In the following collection, this would embolden a person's name while hiding items that are objects: With this, you could watch for specific values in any $scope property. I suppose this could come in very handy at times. Cheerscomplete
" class
is added to the element if(item.Id != 0)
. Alternatively, we could write: 1 !== 0
which is "false
" -- the "DoubleNegative
" class is added to the element.
| | | | | | | |
| |result| |className | | | |
| | | | |
| function | | | condition |
ng-class="{ true: 'highlight', undefined: 'mute' }[ item.hasValue ]"
mute
class will be applied if item has no "hasValue
" property. Furthermore, you can apply a class for any given type or value:{'Jonathan Chapman': 'embolden', '[object Object]': 'hide'}[ item.toString() ]
[
'Jonathan Chapman',
{ aka: 'Johnny Applyseed' },
'Brad Pitt',
{ details: 'Fights Zombies' }
]