What angularjs expression syntax is this in ng-class

前端 未结 5 1262
渐次进展
渐次进展 2021-01-30 13:04

The AngularJS Noob Handbook has some code which reduces class manipulation to a simple expression and binding :

...&         


        
5条回答
  •  日久生厌
    2021-01-30 13:39

    you've probably also seen something like this:

    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:

    . 1 !== 0 which is "false" -- the "DoubleNegative" class is added to the element.

    `

                | |      | |          | | |            |
                | |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

提交回复
热议问题