ngclass expressions in angularjs

原文: 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="[email protected]" data-semver="3.0.3" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
    <script data-require="[email protected]" 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="[email protected]" data-semver="3.0.3" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
    <script data-require="[email protected]" 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="[email protected]" data-semver="3.0.3" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
    <script data-require="[email protected]" 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>
时间: 2024-08-01 06:14:36

ngclass expressions in angularjs的相关文章

Basics of AngularJS

Basics of AngularJS: Part 1 By Gowtham Rajamanickam on Apr 09, 2015 AngularJS I have planned to start writing about AngularJS. I have begun to learn AngularJS a few months back, it's a great feature for website UI developers. It is also easy to learn

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

写在前面 最近在项目中遇到这样的一个前端的bug,在ng-repeat中绑定的图片,有一个晃动的特效,在手机端浏览的时候,图片有时候会正常展示,有时就展示不出来.当时猜测是因为angularjs与特效的那些代码加载的先后顺序造成的.有了这样的猜测,就有查找解决方案的方向了. 系列文章 [Angularjs]ng-select和ng-options [Angularjs]ng-show和ng-hide [Angularjs]视图和路由(一) [Angularjs]视图和路由(二) [Angular

[Angularjs]国际化

写在前面 在项目中,有用到国际化,跟着就了解了下使用angularjs实现的国际化,这里做一下记录. 系列文章 [Angularjs]ng-select和ng-options [Angularjs]ng-show和ng-hide [Angularjs]视图和路由(一) [Angularjs]视图和路由(二) [Angularjs]视图和路由(三) [Angularjs]视图和路由(四) [Angularjs]ng-class,ng-class-even,ng-class-odd [Angular

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

写在前面 最近在弄文档库的H5版,就查找了下相关的上传组件,发现了ng-upload的东东,推荐给大家. 系列文章 [Angularjs]ng-select和ng-options [Angularjs]ng-show和ng-hide [Angularjs]视图和路由(一) [Angularjs]视图和路由(二) [Angularjs]视图和路由(三) [Angularjs]视图和路由(四) [Angularjs]ng-class,ng-class-even,ng-class-odd [Angul

[Anuglrjs]系列——学习与实践

写在前面 这个系列是来这家公司到现在,边用边学,以及在工作中遇到的问题进行的总结.深入的东西不多,大多是实际开发中用到的东西. 这里做一个目录,方便查看. 系列文章 [Angularjs]ng-select和ng-options [Angularjs]ng-show和ng-hide [Angularjs]视图和路由(一) [Angularjs]视图和路由(二) [Angularjs]视图和路由(三) [Angularjs]视图和路由(四) [Angularjs]ng-class,ng-class

angularJs中关于ng-class的三种使用方式说明

在开发中我们通常会遇到一种需求:一个元素在不同的状态需要展现不同的样子. 而在这所谓的样子当然就是改变其css的属性,而实现能动态的改变其属性值,必然只能是更换其class属性 这里有三种方法: 第一种:通过数据的双向绑定(不推荐) 第二种:通过对象数组 第三种:通过key/value 下面简单说下这三种: 第一种:通过数据的双向绑定 实现方式: function changeClass(){   $scope.className = "change2"; } <div clas

AngularJS的ng-class切换class

在angular中为我们提供了3种方案处理class: 1:scope变量绑定 2:字符串数组形式. 3:对象key/value处理. 第一种我们不推荐使用,看看其他两种解决方案: 字符串数组形式 字符串数组形式是针对class简单变化,具有排斥性的变化,true是什么class,false是什么class,其形如; <span ng-class="{true: 'btn01 hover', false: 'btn01'}[isActive]" ng-click="is

[AngularJS] Using AngularJS&#39;s ngClass

.blue{ color: blue } .bold{ font-weight: bold; } .large{ font-size: 40px; } ngClass can accept an array: <div ng-claass="[blue, large, boild]">ngClass</div> ngClass can take a string: <div ng-claass="blue large boild">

AngularJS 指令ng-class

使用g-class 动态设置元素的类,方法是绑定一个代表所有需要添加的类的表达式.重复的类不会添加.当表达式发生变化,先前添加的类会被移除,新类会被添加. <!doctype html> <html ng-app="myApp"> <head> <link rel="stylesheet" href="//cdn.jsdelivr.net/foundation/4.3.2/css/foundation.min.cs