指令,我将其理解为AngularJS操做HTML element的一种途径。
因为学习AngularJS的第一步就是写内置指令ng-app
以指出该节点是应用的根节点,因此指令早已不陌生。
html
这篇日志简单记录了一些内置指令,先使用起来,再谈一些有趣的东西。
git
全部的内置指令的前缀都为ng
,不建议自定义指令使用该前缀,以避免冲突。
首先从一些常见的内置指令开始。
先列出一些关键的内置指令,顺便简单说说做用域的问题。
angularjs
将表单控件和当前做用域的属性进行绑定,这么解释彷佛也不太正确。
但先不要管咬文嚼字,用起来却是易懂,例如:github
<input type="text" ng-model="someModel.someProperty" /><br> {{someModel.someProperty}}
该指令被调用时会初始化内部做用域。
这个指令通常会出如今比较小的应用中,好比给个demo什么的... app
<div ng-init="job='fighter'"> I'm a/an {{job}} </div>
除了ng-init
,咱们还有更多更好的选择。ide
每一次用AngularJS都离不开这个指令,顺便说下$rootScope
。
声明了ng-app
的元素会成为$rootScope
的起点,而$rootScope
是做用域链的根
,一般声明在<html>
你懂的。
也就是说根
下的做用域均可以访问它。
可是,不建议过分使用$rootScope
,省得全局变量满天飞,效率又差又难管。
下面是一个例子:学习
<html ng-app="myApp"> <body> {{ someProperty }} </body> <script> var myApp = angular.module('myApp', []) .run(function($rootScope) { $rootScope.someProperty = 'hello computer'; }); </script> </html>
咱们用这个指令在一个DOM元素上装上controller。
一个控制器? 确实,从字面意思上这样理解却是不错,那咱们为何须要控制器?
记得AngularJS 1.2.x时还能够这样定义controller来着...ui
function ohMyController($scope) { //... }
AngularJS 1.3.x中禁止了这种方式,由于这种方式会让controller满天飞,分不清层次,全部东西都挂在$rootScope
上...
ng-controller
必须有一个表达式做为参数,另外经过$scope
来继承上级$scope
的方法和属性什么的,$rootScope
也包括在内。
下面只是一个简单的例子,ancestor没法访问child的做用域。 google
<div ng-controller="AncestorController"> {{ ancestorName }} {{ childName }} <div ng-controller="ChildController"> {{ ancestorName }} {{ childName }} </div> </div> <script> var myApp = angular.module('myApp', []) .controller('ChildController', function($scope) { $scope.childName = 'child'; }) .controller('AncestorController', function($scope) { $scope.ancestorName = 'ancestor'; }); </script>
做用域的问题远不止如此,暂且搁下,继续看看其余内置指令。日志
起初不明白为何会有个表单指令,<form>
标签感受也够用啊。
以表单验证为例,在上一篇http://www.cnblogs.com/Kavlez/p/angularjsformvalidation.html中有这么一段代码:
<input type="submit" ng-disabled="mainForm.$invalid" />
也就是表单的状态为$invalid
时禁用提交按钮。
若是场景再稍微复杂一点点,好比一个父表单中有多个子表单,子表单中有3个验证经过时父表单即可以提交。
可是,<form>
是不能够嵌套的。
考虑到这种场景,咱们便使用ng-form
指令来解决这一问题。
例如:
<form name="mainForm" novalidate> <div ng-form="form1"> 姓名:<input type="text" ng-required="true" ng-model="name"/><br> 证件号码:<input type="number" ng-minLength="15" ng-maxLength="18" ng-required="true" ng-model="idnum"/> </div> <br> <div ng-form="form2"> 监护人姓名:<input type="text" ng-required="true" ng-model="gname"/><br> 监护人证件号码:<input type="number" ng-minLength="15" ng-maxLength="18" ng-required="true" ng-model="gidnum"/> </div> <button ng-disabled="form1.$invalid && form2.$invalid">submit all</button> </form>
像这种只要出现则生效的属性,咱们能够在AngularJS中经过表达式返回值true/false令其生效。
禁用表单输入字段。
<textarea ng-disabled="1+1==2">1+1=?</textarea>
经过表达式返回值true/false将表单输入字段设为只读。
弄个例子,3秒后变成只读.
<input type="text" ng-readonly="stopTheWorld" value="stop the world after 3s"/> .run(function($rootScope,$timeout){ $rootScope.stopTheWorld=false; $timeout(function(){ $rootScope.stopTheWorld = true; },3000) })
这个是给<input type="checkbox" />
用的,好比...
<input type="checkbox" ng-checked="someProperty" ng-init="someProperty = true" ng-model="someProperty">
给<select>
里面的<option>
用的,例子:
<label> <input type="checkbox" ng-model="isFullStack"> I'm Full Stack Engineer </label> <select> <option>Front-End</option> <option>Back-End</option> <option ng-selected="isFullStack">Full Stack !!!</option> </select>
根据表达式显示/隐藏HTML元素,注意是隐藏,不是从DOM移除,例如:
<div ng-show="1+1 == 2"> 1+1=2 </div> <div ng-hide="1+1 == 3"> you can't see me. </div>
不是HTML那套onXXX
之类的,而是ng-XXX
。
结合ng-model
使用,以ng-change
为例:
<input type="text" ng-model="calc.arg" ng-change="calc.result = calc.arg*2" /> <code>{{ calc.result }}</code>
或者好比ng-options
其实这个也是一个指令,也许以为和ng-bind
差很少,但页面渲染略慢时可能会被看到。
另外,{{}}
的performance远不如ng-bind
,只是用起来很方便。
ng-bind
的行为和{{}}
差很少,只是咱们能够用这个指令来避免FOUC(Flash Of Unrendered Content),也就是未渲染致使的闪烁。
ng-cloak
也能够为咱们解决FOUC。 ng-cloak
会将内部元素隐藏,直到路由调用对应的页面。
若是ng-if中的表达式为false,则对应的元素整个会从DOM中移除而非隐藏,但审查元素时你能够看到表达式变成注释了。
若是相进行隐藏,可使用ng-hide
。
<div ng-if="1+1===3"> 没法审查到该元素 </div> <div ng-hide="1+1==2"> 可审查 </div>
单独使用没什么意思,下面是例子:
<div ng-switch on="1+1"> <p ng-switch-default>0</p> <p ng-switch-when="1">1</p> <p ng-switch-when="2">2</p> <p ng-switch-when="3">3</p> </div>
不明白为毛不叫iterate,总之是遍历集合,给每一个元素生成模板实例,每一个实例的做用域中能够用一些特殊属性,以下:
不用特意解释,这些都很容易看出来是干什么的,下面是一个例子:
<ul> <li ng-repeat="char in [{'alphabet': 'K'}, {'alphabet': 'A'}, {'alphabet': 'V'}, {'alphabet': 'L'}, {'alphabet': 'E'}, {'alphabet': 'Z'}] " ng-show="$even">{{char.alphabet}}</li> </ul>
起初我在一个文本域中弄了个ng-model,而后像这样<a href="{{myUrl}}">
在href里面写了进去。
其实这样href
和ng-href
看不出什么区别,因此咱们能够试试这样:
<ul ng-init="myHref=''"> <li><a ng-href="{{ myHref }}">{{linkText}}</a></li> <li><a href="{{ myHref }}">能够点击,但不必定是正确的地址</a></li> </ul> .run(function($rootScope, $timeout) { $rootScope.linkText = '还没有加载,您没法点击'; $timeout(function() { $rootScope.linkText = '请点击' $rootScope.myHref = 'http://google.com'; }, 2000); })
大同小异,即表达式生效前不要加载该资源。
例子(ps: 图片不错! ):
<img ng-src="{{imgSrc}}"/> .run(function($rootScope, $timeout) { $timeout(function() { $rootScope.imgSrc = 'https://octodex.github.com/images/daftpunktocat-guy.gif'; }, 2000); })
用做用域中的对象动态改变类样式,例如:
<style> .red {background-color: red;} .blue {background-color: blue;} </style> <div ng-controller="CurTimeController"> <button ng-click="getCurrentSecond()" >Get Time!</button> <p ng-class="{red: x%2==0,blue: x%2!=0}" >Number is: {{ x }}</p> </div> .controller('CurTimeController', function($scope) { $scope.getCurrentSecond = function() { $scope.x = new Date().getSeconds(); }; })