Ngclass Expressions in Angularjs

Source: Internet
Author: User
Tags button type

Original: http://blog.xebia.com/2014/01/31/ngclass-expressions-in-angularjs/

The ngclass directive allows you to dynamically set CSS classes by databinding an expression.

String Syntax

The string syntax is very simple and straightforward, and the following code directly adds a text class to the legend element by ng-class= "text" .

<! DOCTYPE html>
Array Syntax

The

The array syntax is similar to string syntax, but the array syntax allows you to add more than one CSS class to an HTML element at a time (ng-class= "[Label, Text]").

<! DOCTYPE html>Map Syntax

Map syntax allows you to set CSS classes by separating key-value pairs with commas. In the following example, when the value of info is true, Lable-info is added to the legend element. If the value of muted is true, text-muted is added to the legend element.

<! DOCTYPE html>
Undocumented Expression Syntax

In the following example, when the controller is called for the first time, the value of the submitted variable is false. The submitted variable is set to True when the form is submitted. Next we check if the form is legal. If the form is illegal, return directly.

' Use strict '; Angular.module (' myApp ', []).  Controller (' Myappctrl ', function () {    this.submitted = false;         var = this;    This.submit = function (form) {      self.submitted = true;      if (form. $invalid) {        return;      } else {        //something with the form like posting it to the backend      }
   
    }  });
   

So how do we write an expression if submmited is true and the value of the input element is illegal, add a class. (ng-class= "{true: ' Has-error '}[ctrl.submitted && Myform.myfield. $error. Required] ")

<!doctype html>

Ngclass Expressions in Angularjs

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.