The use of Ng-class

Source: Internet
Author: User

Recently in the learning angular framework, ng-class is an instruction in the angular framework, and here is the official explanation of the Ng-class directive:

The ng-class directive is used to dynamically bind one or more CSS classes to an HTML element.

ng-classThe value of the directive can be a string, an object, or an array.

If it is a string, multiple class names are separated by spaces.

If it is an object, you need to use the key-value pair, and key is a Boolean value, which is the class name you want to add. The class is added only if key is true.

If it is an array, it can consist of a string or an object combination, and the elements of the array can be strings or objects.

There are three ways to handle class in angular:
1:scope variable bindings.
2: string array form.
3: Object Key/value processing.

Example 1: (Scope variable binding form)

function Test ($scope) {   $scope. Test = "classname";} <  class= "{{test}}"></div>

It is not recommended in this way.

Example 2: (String array form)

function Ctr ($scope) {     $scope. isActive = true;} <  ng-class= "{true: ' active ', false: ' inactive '}[isactive]"></ Div >

This code means that when the value of IsActive is true, the dynamically bound class is active, and for Flase, dynamically bound classes are inactive, which can be separated by a space when multiple class names need to be bound.

Example 3: (Object Key/value processing)

You need to use Key-value pair, key is a Boolean value, and value is the class name you want to add. The class is added only if key is true.

function Ctr ($scope) {} <  ng-class {' selected ': isSelected, ' car ': IsCar} '></div> 

In fact, these and arrays are similar in string mode, when IsSelected is true, the Select class is dynamically bound, and when Iscar is true, the car class is bound dynamically.

The use of Ng-class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.