Angularjs's Ng-class (11)

Source: Internet
Author: User

Objective

In this section we will talk about the ng-class in the instruction, by adding ng-class to generate the corresponding style, sometimes we have to choose the corresponding style according to different conditions, this section we look at the flexibility of ng-class usage.

Ng-class using Strings (i)

Presumably this usage is the simplest one, let's take a little demo.

<Divclass= "Well">    <Divclass= "Form-group">        <inputtype= "text"placeholder= "Input your class type"Ng-model= "Texttype"class= "Form-control" />    </Div>    <DivNg-class= "Texttype"style= "Font-size:x-large;">xpy0928 from Cnblogs</Div></Div>

Effect:

Ng-class using Arrays (ii)

Ng-class as an array to select the style is implemented by [], as follows:

<Divclass= "Well">    <inputtype= "text"Ng-model= "Styleone"class= "Form-control">    <inputtype= "text"Ng-model= "Styletwo"class= "Form-control">    <DivNg-class= "[Styleone, Styletwo]"style= "Font-size:x-large;">xpy0928 from Cnblogs</Div></Div>

Effect:

Ng-class using an evaluation expression (iii)

What evaluates an expression is to display the corresponding style when the variable we define is true, otherwise it is not applied. Let's take a look at the code:

<Divclass= "Well">    <inputtype= "checkbox"Ng-model= "Danger">good luck to u? <inputtype= "checkbox"Ng-model= "wrong">u are wrong? <BR/>    <spanNg-class= "{' Btn-danger ': danger, ' btn-success ': Wrong}"style= "Font-size:x-large;">xpy0928 from Cnblogs</span></Div>

The evaluation expression must be represented by the {}, which is relatively advanced in several ways, compared to the Ng-class usage. Let's look at the actual effect:

Let's also cite an example of coloring different lines in the display list.

<Divclass= "Well">    <ul>        <LiNg-class= "{' Text-info ': $even, ' Text-danger ': $odd}"ng-repeat= "Item in Items">{{Item.name}}</Li>    </ul></Div>

Effect:

Ng-class using three-dimensional expressions (iv)

Ternary operators do not have to introduce the bar, we directly on the code to demonstrate the effect:

<Divclass= "Well">    <ul>        <LiNg-class= "$even? ' Btn-info ': ' Btn-danger ' "ng-repeat= "Item in Items">{{Item.name}}</Li>    </ul></Div>

Effect:

Summarize

In this section we talk about the Ng-class directive, by giving several different ways of using it to illustrate its flexibility, to tell us to choose the right scenario for you in different situations.

Angularjs's Ng-class (11)

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.