you've probably also seen something like this:
<div ng-class="{true: 'complete'}[item.Id != 0]"></div>
Very rad syntax.
EDIT:
What happens here, is that the "complete
" class
is added to the element if(item.Id != 0)
. Alternatively, we could write: <div ng-class="{false: 'cookieless'}[monsterEatsCookies('Elmo')]
. As its decided by the monsterEatsCookies function, Elmo does not eat cookies so since this function returns false
the html element gains a class called cookieless
.
A simple example: <div ng-class="{false: 'DoubleNegative'}[1 == 0]
. 1 !== 0
which is "false
" -- the "DoubleNegative
" class is added to the element.
<div ng-class="{ true: 'complete' } [item.Id != 0]"></div>
`
| | | | | | | |
| |result| |className | | | |
| | | | |
| function | | | condition |
Addendum
Also, I just realized that you can use a variety of different keys to map to your condition. For example:
ng-class="{ true: 'highlight', undefined: 'mute' }[ item.hasValue ]"
The 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() ]
In the following collection, this would embolden a person's name while hiding items that are objects:
[
'Jonathan Chapman',
{ aka: 'Johnny Applyseed' },
'Brad Pitt',
{ details: 'Fights Zombies' }
]
With this, you could watch for specific values in any $scope property. I suppose this could come in very handy at times.
Cheers