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

ngClass 指令允许你通过databinding一个表达式来动态的设置CSS类.

String Syntax

string syntax非常简单、直接, 下面的代码通过ng-class="text"直接添加一text类到legend元素.

<!DOCTYPE html>
<html ng-app> <head>
<link data-require="bootstrap-css@3.0.3" data-semver="3.0.3" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
<script data-require="angular.js@1.2.10" data-semver="1.2.10" src="http://code.angularjs.org/1.2.10/angular.js"></script>
</head> <body>
<div class="container">
<div class="row">
<form role="form">
<fieldset>
<legend ng-class="text">String syntax</legend>
<div class="form-group">
<input class="form-control" ng-model="text" placeholder="Type: text-info, text-warning or text-danger"><br>
</div>
</fieldset>
</form>
</div>
</div>
</body> </html>

Array Syntax

The array syntax类似于string syntax, 但是Array Syntax能让你一次添加多个CSS类到HTML元素(ng-class="[label, text]").

<!DOCTYPE html>
<html ng-app> <head>
<link data-require="bootstrap-css@3.0.3" data-semver="3.0.3" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
<script data-require="angular.js@1.2.10" data-semver="1.2.10" src="http://code.angularjs.org/1.2.10/angular.js"></script>
</head> <body>
<div class="container">
<div class="row">
<form role="form">
<fieldset>
<legend ng-class="[label, text]">Array syntax</legend>
<div class="form-group">
<input class="form-control" ng-model="label" placeholder="Type: label-info, label-warning or label-danger"><br>
<input class="form-control" ng-model="text" placeholder="Type: text-muted or text-success"><br>
</div>
</fieldset>
</form>
</div>
</div>
</body> </html>

Map Syntax

map syntax能让你通过逗号分隔键值对设置CSS类. 下面的例子中, 当info的值为true时lable-info会被添加到legend元素. 如果muted的值为true时text-muted会被添加到legend元素.

<!DOCTYPE html>
<html ng-app> <head>
<link data-require="bootstrap-css@3.0.3" data-semver="3.0.3" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
<script data-require="angular.js@1.2.10" data-semver="1.2.10" src="http://code.angularjs.org/1.2.10/angular.js"></script>
</head> <body>
<div class="container">
<div class="row">
<fieldset>
<legend ng-class="{'label-info': info, 'text-muted': muted}">Map syntax</legend>
<div class="form-group">
<input type="checkbox" ng-model="info"> info (apply "label-info" class)<br>
<input type="checkbox" ng-model="muted"> muted (apply "text-muted" class)
</fieldset>
</div>
</div>
</body> </html>

Undocumented Expression Syntax

下面的例子中, 当controller第一次被调用的时候submitted变量的值为false. 当form提交的时候submitted变量设置为true. 接下来我们检查form是否合法. 如果form非法直接return.

'use strict';

angular.module('myApp', []).
controller('MyAppCtrl', function() {
this.submitted = false; var self = this;
this.submit = function(form) {
self.submitted = true;
if (form.$invalid) {
return;
} else {
// Do something with the form like posting it to the backend
}
}
});

那么我们怎么写一个表达式 如果submmited为true并且input元素的值非法的时候添加一个class.(ng-class="{true: 'has-error'}[ctrl.submitted && myForm.myField.$error.required]")

<!doctype html>
<html ng-app="myApp">
<head>
<link src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://code.angularjs.org/1.2.10/angular.min.js"></script>
</head>
<body ng-controller="MyAppCtrl as ctrl">
<div class="container">
<div class="row">
<form class="form-horizontal" name="myForm" novalidate>
<fieldset>
<div class="form-group" ng-class="{true: 'has-error'}[ctrl.submitted && myForm.myField.$error.required]">
<label for="myField" class="control-label">My Field</label>
<input type="text" name="myField" class="form-control" id="myField" ng-model="myField" required/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" ng-click="ctrl.submit(myForm)">Save</button>
</div>
</fieldset>
</form>
</div>
</div>
<script src="script.js"></script>
</body>
</html>

[译]ngclass expressions in angularjs的更多相关文章

  1. [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序

    本文转自:http://www.skcode.cn/archives/281 本文演示ABP框架如何使用AngularJs,ASP.NET MVC,Web API 和EntityFramework构建 ...

  2. [Angularjs]系列——学习与实践

    写在前面 这个系列是来这家公司到现在,边用边学,以及在工作中遇到的问题进行的总结.深入的东西不多,大多是实际开发中用到的东西. 这里做一个目录,方便查看. 系列文章 [Angularjs]ng-sel ...

  3. [Angularjs]国际化

    写在前面 在项目中,有用到国际化,跟着就了解了下使用angularjs实现的国际化,这里做一下记录. 系列文章 [Angularjs]ng-select和ng-options [Angularjs]n ...

  4. [Angularjs]ng-repeat中使用ng-model遇到的问题

    写在前面 在ng-reapet中如何为ng-model双向绑定呢?在项目中确实遇到这样的问题,绑定了,但是在controller中获取不到它的值,确实挺奇怪的. 系列文章 [Angularjs]ng- ...

  5. [Angularjs]ng-file-upload上传文件

    写在前面 最近在弄文档库的H5版,就查找了下相关的上传组件,发现了ng-upload的东东,推荐给大家. 系列文章 [Angularjs]ng-select和ng-options [Angularjs ...

  6. [Angularjs]angular ng-repeat与js特效加载先后导致的问题

    写在前面 最近在项目中遇到这样的一个前端的bug,在ng-repeat中绑定的图片,有一个晃动的特效,在手机端浏览的时候,图片有时候会正常展示,有时就展示不出来.当时猜测是因为angularjs与特效 ...

  7. AngularJS基础总结

    w3shools    angularjs教程  wiki   <AngularJS权威教程> Introduction AngularJS is a JavaScript framewo ...

  8. AngularJS–Animations(动画)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/   在AngularJS 1.3 中,给一些指令(eg:   ngRepeat,ngSw ...

  9. AngularJs(Part 7)--Build-in Directives

    Directives In AngularJS, we can use a variety of naming conventions to reference directives . In the ...

随机推荐

  1. linux内核增加系统调用--Beginner‘s guide

    Linux内核中设置了一组用于实现系统功能的子程序,称为系统调用.系统调用和普通库函数调用非常相似明知是系统调用由操作系统核心提供,运行于核心态,而普通的函数调用由函数库或用户自己提供,运行于用户态. ...

  2. install docker on xubuntu

    ref: https://docs.docker.com/engine/installation/linux/ubuntulinux/#/install-the-latest-version ps: ...

  3. hdu 5017 模拟退火

    题意:给出椭球面的立体解析式,要求椭球面上距离原点最近的点的距离 sol:这题要想推公式就

  4. phpMyadmin /scripts/setup.php Execute Arbitrary PHP Code Via A Crafted POST Request CVE-2010-3055

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 The configuration setup script (aka scrip ...

  5. Refresh和Invalidate的比较

    Refresh: 强制控件使其工作区无效并立即重绘自己和任何子控件.==Invalidate + Update Invalidate: 使控件的特定区域(可以自己设置区域,从而提高性能)无效并向控件发 ...

  6. 屠蛟之路_乘风破浪_FifthDay

    乘风破浪 一艘大船 (我们写的几千行代码) 一片汪洋 (软工耗费的时间) 一群骚年 (一不小心就火了) 为救一位公主 (吃自助餐) 选择去跋山涉水,乘风破浪(解决bug,解决冲突) 毫不畏惧 (韧性) ...

  7. 【转】python编码规范

    http://blog.csdn.net/willhuo/article/details/49300441 决定开始Python之路了,利用业余时间,争取更深入学习Python.编程语言不是艺术,而是 ...

  8. jQuery 基础(4)jQuery 尺寸

    jQuery 尺寸方法jQuery 提供多个处理尺寸的重要方法:width()height()innerWidth()innerHeight()outerWidth()outerHeight()jQu ...

  9. php 数据访问(以mysql数据库为例)

    //建一个连接,造一个连接对象$db = new MySQLi("localhost","root","123","mydb&qu ...

  10. tomcat7.0配置CORS(跨域资源共享)

    平时我们做前台页面时可能会遇到浏览器以下提示(浏览器控制台): 已阻止跨源请求:同源策略禁止读取位于 http://xxx.xxx.com 的远程资源.(原因:CORS 头缺少 'Access-Con ...