ngClass (directive)

Improve this Doc View Source ngClass

  1. directive in module ng

The ngClass directive allows you to dynamically set CSS classes on an HTML element by databinding an expression that represents all classes to be added.

The directive operates in three different ways, depending on which of three types the expression evaluates to:

  1. If the expression evaluates to a string, the string should be one or more space-delimited class names.

  2. If the expression evaluates to an array, each element of the array should be a string that is one or more space-delimited class names.

  3. If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name.

The directive won't add duplicate classes if a particular class was already set.

When the expression changes, the previously added classes are removed and only then the new classes are added.

Directive Info

  • This directive executes at priority level 0.

Usage

  • as attribute:
    <ANY
      ng-class="">
    ...
    </ANY>
  • as CSS class:
    <ANY class="ng-class: ;"> ... </ANY>

Animations

add - happens just before the class is applied to the element remove - happens just before the class is removed from the element

Click here to learn more about the steps involved in the animation.

Arguments

Param Type Details
ngClass expression

Expression to eval. The result of the evaluation can be a string representing space delimited class names, an array, or a map of class names to boolean values. In the case of a map, the names of the properties whose values are truthy will be added as css classes to the element.

Example that demonstrates basic bindings via ngClass directive.

Animations

The example below demonstrates how to perform animations using ngClass.

ngClass and pre-existing CSS3 Transitions/Animations

The ngClass directive still supports CSS3 Transitions/Animations even if they do not follow the ngAnimate CSS naming structure. Upon animation ngAnimate will apply supplementary CSS classes to track the start and end of an animation, but this will not hinder any pre-existing CSS transitions already on the element. To get an idea of what happens during a class-based animation, be sure to view the step by step details of $animate.addClass and $animate.removeClass.

© 2010–2016 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.2.32/docs/api/ng/directive/ngClass

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部