【js类库AngularJs】学习angularJs的指令(包括常见表单验证,隐藏等功能)

AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购。是一款优秀的前端JS框架,已经被用于Google的多款产品当中。AngularJS有着诸多特性,最为核心的 是:MVVM、模块化、自动化双向数据绑定、语义化标签、依赖注入、等等。

参考资料:

angularjs中文网:http://www.apjs.net/

angularjs官网:http://angularjs.org

api参考:http://docs.angularjs.cn/api/ng/directive/form

(之所以列出如上这些链接,是因为本人在学习各类语言的过程中,发现入门以后,通过看官方文档,反倒会学习的更快,更准确,更系统。)

从jquery转向angularJs这种双向绑定的js框架时,最让人担忧的是原来处理dom的方式在使用angularJs后,应该怎么适应呢?

本文列出了比较常见的几种处理,包括表单验证,动态设置元素样式,设置元素显示隐藏等功能。

1. input[checkbox]双选框

使用方法:

<input type="checkbox"
ng-model=""
[name=""]
[ng-true-value=""]
[ng-false-value=""]
[ng-change=""]>

使用实例:

<form name="myForm" ng-controller="ExampleController">
Value1: <input type="checkbox" ng-model="value1"> <br/>
Value2: <input type="checkbox" ng-model="value2"
ng-true-value="'YES'" ng-false-value="'NO'"> <br/>
<tt>value1 = {{value1}}</tt><br/>
<tt>value2 = {{value2}}</tt><br/>
</form>

js代码:

 var app = angular.module('formExample', []);
app.controller('ExampleController', ['$scope', function($scope) {
console.log('...');
$scope.value1 = true;
$scope.value2 = 'YES'
}]);

页面效果图:

2.input[date]时间选择器

使用方法:

<input type="date"
ng-model=""
[name=""]
[min=""]
[max=""]
[required=""]
[ng-required=""]
[ng-change=""]>

使用实例:

<form name="dateForm" ng-controller="DateController">
Pick a date in 2015:
<input type="date" id="exampleInput" name="input" ng-model="value"
placeholder="yyyy-MM-dd" min="2015-01-01" max="2015-12-31" required />
<span class="error" ng-show="dateForm.input.$error.required">
Required!</span>
<span class="error" ng-show="dateForm.input.$error.date">
Not a valid date!</span>
<tt>value = {{value}}</tt><br/>
<tt>value = {{value | date: "yyyy-MM-dd"}}</tt><br/>
<tt>dateForm.input.$valid = {{dateForm.input.$valid}}</tt><br/>
<tt>dateForm.input.$error = {{dateForm.input.$error}}</tt><br/>
<tt>dateForm.$valid = {{dateForm.$valid}}</tt><br/>
<tt>dateForm.$error.required = {{!!dateForm.$error.required}}</tt><br/>
</form>

js代码:

function DateController($scope){
$scope.value = new Date(2015, 9, 22);
}

效果图:

3.ng-class 使用angularJs动态设置、更改dom元素的css样式。

css样式:

.strike {
text-decoration: line-through;
}
.bold {
font-weight: bold;
}
.red {
color: red;
}

html代码:

    <p ng-class="{strike: deleted, bold: important, red: error}">Map Syntax Example</p>
<input type="checkbox" ng-model="deleted"> deleted (apply "strike" class)<br>
<input type="checkbox" ng-model="important"> important (apply "bold" class)<br>
<input type="checkbox" ng-model="error"> error (apply "red" class)
<hr>
<p ng-class="style">Using String Syntax</p>
<input type="text" ng-model="style" placeholder="Type: bold strike red">
<hr>
<p ng-class="[style1, style2, style3]">Using Array Syntax</p>
<input ng-model="style1" placeholder="Type: bold, strike or red"><br>
<input ng-model="style2" placeholder="Type: bold, strike or red"><br>
<input ng-model="style3" placeholder="Type: bold, strike or red"><br>

效果图:

4.ng-class-odd/even 分别定义奇数偶数元素的css样式

css样式:

.odd {
color: red;
}
.even {
color: blue;
}

html代码:

    <ol ng-init="names=['John', 'Mary', 'Cate', 'Suz']">
<li ng-repeat="name in names">
<span ng-class-odd="'odd'" ng-class-even="'even'">
{{name}} &nbsp; &nbsp; &nbsp;
</span>
</li>
</ol>

效果图:

5.ng-click 单击事件

    <button ng-click="count = count + 1" ng-init="count=0">
Increment
</button>
<span>
count: {{count}}
</span>

6.ng-show/hide 是否显示,或隐藏。angularJs根据这两个属性的值,对相应元素增加.ng-hide的样式

css样式:

.ng-hide {
line-height:;
opacity:;
padding: 0 10px;
}

html代码:

    <span>Click me: </span><input type="checkbox" ng-model="checked"><br/>
<div>
Show:
<div style="background-color:#ccc" ng-show="checked">
<span> I show up when your checkbox is checked.</span>
</div>
</div>
<div>
Hide:
<div style="background-color:#258be3;color:white" ng-hide="checked">
<span> I hide when your checkbox is checked.</span>
</div>
</div>

最后,全部代码如下,单页test.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>angularJs directive(指令)</title>
<style>
body{font:12px/1.5 tahoma,arial,'Hiragino Sans GB',\5b8b\4f53,sans-serif}
.strike {
text-decoration: line-through;
}
.bold {
font-weight: bold;
}
.red {
color: red;
}
.odd {
color: red;
}
.even {
color: blue;
}
.ng-hide {
line-height: 0;
opacity: 0;
padding: 0 10px;
}
</style>
<script src="//cdn.bootcss.com/angular.js/1.2.19/angular.js"></script>
</head>
<body ng-app="formExample"> <h2>AngularJS Sample Application</h2>
<div>
<h3>input[checkbox]</h3>
<form name="myForm" ng-controller="ExampleController">
Value1: <input type="checkbox" ng-model="value1"> <br/>
Value2: <input type="checkbox" ng-model="value2"
ng-true-value="'YES'" ng-false-value="'NO'"> <br/>
<tt>value1 = {{value1}}</tt><br/>
<tt>value2 = {{value2}}</tt><br/>
</form>
</div> <div>
<hr>
<h3>input[date]</h3>
<form name="dateForm" ng-controller="DateController">
Pick a date in 2015:
<input type="date" id="exampleInput" name="input" ng-model="value"
placeholder="yyyy-MM-dd" min="2015-01-01" max="2015-12-31" required />
<span class="error" ng-show="dateForm.input.$error.required">
Required!</span>
<span class="error" ng-show="dateForm.input.$error.date">
Not a valid date!</span>
<tt>value = {{value}}</tt><br/>
<tt>value = {{value | date: "yyyy-MM-dd"}}</tt><br/>
<tt>dateForm.input.$valid = {{dateForm.input.$valid}}</tt><br/>
<tt>dateForm.input.$error = {{dateForm.input.$error}}</tt><br/>
<tt>dateForm.$valid = {{dateForm.$valid}}</tt><br/>
<tt>dateForm.$error.required = {{!!dateForm.$error.required}}</tt><br/>
</form>
</div>
<script>
var app = angular.module('formExample', []);
app.controller('ExampleController', ['$scope', function($scope) {
console.log('...');
$scope.value1 = true;
$scope.value2 = 'YES'
}]); /* app.controller('DateController', ['$scope', function($scope) {
$scope.value = new Date(2015, 9, 22);
console.log($scope.value);
}]);*/
//上下两种写法都可以
function DateController($scope){
$scope.value = new Date(2015, 9, 22);
}
</script>
<div>
<hr>
<h3>ng-class</h3>
<p ng-class="{strike: deleted, bold: important, red: error}">Map Syntax Example</p>
<input type="checkbox" ng-model="deleted"> deleted (apply "strike" class)<br>
<input type="checkbox" ng-model="important"> important (apply "bold" class)<br>
<input type="checkbox" ng-model="error"> error (apply "red" class)
<hr>
<p ng-class="style">Using String Syntax</p>
<input type="text" ng-model="style" placeholder="Type: bold strike red">
<hr>
<p ng-class="[style1, style2, style3]">Using Array Syntax</p>
<input ng-model="style1" placeholder="Type: bold, strike or red"><br>
<input ng-model="style2" placeholder="Type: bold, strike or red"><br>
<input ng-model="style3" placeholder="Type: bold, strike or red"><br>
</div>
<div>
<hr>
<h3>ng-class-odd/even</h3>
<ol ng-init="names=['John', 'Mary', 'Cate', 'Suz']">
<li ng-repeat="name in names">
<span ng-class-odd="'odd'" ng-class-even="'even'">
{{name}} &nbsp; &nbsp; &nbsp;
</span>
</li>
</ol>
</div>
<div><hr>
<h3>ng-click</h3>
<button ng-click="count = count + 1" ng-init="count=0">
Increment
</button>
<span>
count: {{count}}
</span>
</div>
<div><hr>
<h3>ng-show/hide</h3>
<span>Click me: </span><input type="checkbox" ng-model="checked"><br/>
<div>
Show:
<div style="background-color:#ccc" ng-show="checked">
<span> I show up when your checkbox is checked.</span>
</div>
</div>
<div>
Hide:
<div style="background-color:#258be3;color:white" ng-hide="checked">
<span> I hide when your checkbox is checked.</span>
</div>
</div>
</div>
<br/>
</body>
</html>

目前我了解到,有不少人使用Angular+ionic开发html5版本的app。
更多细节可以参考网站http://www.ionic.wang/start-index.html

另外,还有免费教程: http://www.ionic.wang/course-index.html

推荐的网站:
http://docs.angularjs.cn/api/ (api文档)
http://showcase.ngnice.com/#/home/home (常用实例)
http://docs.ngnice.com/guide/expression (开发文档)

【js类库AngularJs】学习angularJs的指令(包括常见表单验证,隐藏等功能)的更多相关文章

  1. jQuery学习之路(8)- 表单验证插件-Validation

    ▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...

  2. js数组,数字函数,字符串函数,表单验证,hashMap,堆栈,日期函数,call函数

    1.javascript的数组API Js代码 收藏代码 //定义数组 var pageIds = new Array(); pageIds.push('A'); 数组长度 pageIds.lengt ...

  3. 【Java EE 学习 33 下】【validate表单验证插件】

    一.validate 1.官方网站:http://jqueryvalidation.org/ 2.文档说明:http://jqueryvalidation.org/documentation/ 3.j ...

  4. JS判断字符串长度,结合element el-input el-form 表单验证(英文占1个字符,中文汉字占2个字符)

    首先看看判断字符串长度的几种方法(英文占1个字符,中文汉字占2个字符) 方法一: function strlen(str) { var len = 0; for (var i = 0; i < ...

  5. angularjs学习第五天笔记(第二篇:表单验证升级篇)

    您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...

  6. angularjs学习第四天笔记(第一篇:简单的表单验证)

    您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...

  7. angularJS中的表单验证(包括自定义验证)

    表单验证是angularJS一项重要的功能,能保证我们的web应用不会被恶意或错误的输入破坏.Angular表单验证提供了很多表单验证指令,并且能将html5表单验证功能同他自己的验证指令结合起来使用 ...

  8. 【AngularJS学习笔记】AngularJS表单验证

    AngularJS表单验证 AngularJS提供了一些自带的验证属性 1.novalidate:添加到HTML的表单属性中,用于禁用浏览器默认的验证. 2.$dirty   表单有填写记录 3.$v ...

  9. AngularJS学习笔记(二) 表单验证案例(ng-repeat/filter)

    这一节相对来说需要理解的东西不是太多,记住了那些api就行了. 还是一个案例(同样来自miaov),一个表单验证,先上代码,然后再对对应的内容进行解释. <!DOCTYPE html> & ...

随机推荐

  1. java---集合类(1)

    java.util包中包含了一系列重要的集合类.而对于集合类,主要需要掌握的就是它的内部结构,以及遍历集合的迭代模式. 接口:Collection Collection是最基本的集合接口,一个Coll ...

  2. 【opencv学习笔记一】opencv下载安装与VS2017开发环境配置

    本文章摘录自浅墨博客,原文链接http://blog.csdn.net/poem_qianmo/article/details/19809337 目录 1.opencv下载与安装 2.计算机环境变量配 ...

  3. 2-7 Flutter开发工具使用指南

    这里选择用哪个模拟器运行 Mac系统下可以通过这个Open IOS Siumlator打开IOS模拟器 debug用来调试的 可以创建新的模拟器 选择安卓模拟器的版本 这是sdk的配置 点开就是打开了 ...

  4. HERO3+ Black Edition 视角 (FOV) 信息

    HERO3+ Black Edition 视角 (FOV) 信息 问题  HERO3+ Black Edition 视角 (FOV) 信息是什么? 它在哪适用? HERO3+ Black 作答 所有视 ...

  5. deb包制作

    制作deb包 方法一:checkinstall checkinstall的原理就是将make install的内容放到一个临时目录然后打包,只要能执行make install就能创建deb包,如果代码 ...

  6. Codeforces617E【莫队算法+前缀异或】

    题意: 给出一系列数,对每个查询区间,计算有多少个子区间异或为k. 思路: 可以先预处理异或前缀,一个区间[L,R]的异或值=sum[R]^sum[L-1]; 如果当前区间是[a,b],加一个右端点b ...

  7. 1366 - Incorrect string value:'\xE5\xBC\xA0\xE4\xB8\x89' for column 'name' a 错误修改

    把name的字符集修改成 utf8 ,然后把表关了从新打开,就可以了 如果还不行,就从新创表,在创表的时候修改name的字符集 如果还不行,就修改my.ini 它在你的mysql安装路径里 [mysq ...

  8. 解决IE6 IE7绝对定位弹层被后面的元素遮住

    解决IE6 IE7绝对定位弹层被后面的元素遮住? 弹层边框一直被后面的元素边框遮住,试了n种方法,只有这个比较好用. <div class="tuijian-table"&g ...

  9. C# Func与Action总结

    Action:无参数无返回值委托. Action<T>:泛型委托,无返回值,根据输入参数的个数不同有十六个重载. Func< out T>:无输入参数,有返回值. Func&l ...

  10. 12.创建高级联结---SQL

    一.使用表别名 SQL除了可以对列名和计算字段使用别名,还允许给表名起别名.这样做有两个主要理由: 缩短SQL语句: 允许在一条SELECT语句中多次使用相同的表. SELECT cust_name, ...