AngularJS 通过被称为 指令 的新属性来扩展 HTML。

正如你所看到的,AngularJS 指令是以 ng 作为前缀的 HTML 属性。

HTML5 允许扩展的(自制的)属性,以 data- 开头。

AngularJS 属性以 ng- 开头,但是您可以使用 data-ng- 来让网页对 HTML5 有效。

以下列举了一些指令,并不一定常用,但是都有用!

有些指令相关的demo,有兴趣的可以下载,github地址:https://github.com/392468072/demo

自带指令

1、ng-app此指令声明angular的边界,也就是应用程序入口(当然没有ng—app指令的时候也不要奇怪,还有其他方法可以做入口,angular.bootstrap(element,[modules],config))

2、ng-bind 就是绑定模板,其实和{{hash}}的效果是一样的,不过要注意的是{{hash}}在各种原因下可能会被用户看到,带来不好的用户体验

3、ng-model 它链接了页面可交互元素(input,textarea之类的)和位于$scope之上的model,这儿有点乱,自己理清楚即可

4、ng-controller设置子作用域对象$scope(父作用域为$rootScope)的初始状态,给子作用域$scope增加行为,控制业务逻辑

5、ng-init 初始化数组,可以方便测试,现在已经不推荐使用了

6、ng-repeat迭代输出 orderBy:“keyword”可以指定输出顺序

7、ng-click 允许自定义行为某个元素被点击,当然其它事件指令都有类似的作用如:

  ng-dbclick双击  ng-mouseDown按下鼠标左键  ng-mouseUp松开鼠标左键  ng-mouseOver鼠标移出  ng-mouseEnter鼠标移入

  ng-copy 文本被复制  ng-paste文本被粘贴  ng-select文本被选择  ng-change ng-blur ng-keydown等等,当然还有很多,就不11列出了

8、ng-submit 这肯定和form表单相关咯,对,就是submit form的意思

9、ng-href/ng-src 链接,这里可能会有人问,为什么不直接用html标签的属性呢?href/src = “{{hash}}”

  因为官方说:The browser will fetch from the URL with the literal text {{hash}} until Angular replaces the expression inside {{hash}}.

  如果浏览器将{{hash}}里的值,也即src/href属性值替换成文本以后可能就停止干活了.

10、ng-if 接收boolean值,若为false,它控制的DOM节点会被删除,若为true就会创建被插入DOM节点

11、ng-include 可以在模板中嵌入其他模板,实现前端页面的复用

12、ng-non-bindable指令指该元素的内部{{****}}不被绑定和转义,不受angular的掌控

13、ng-pluralize 应该是使成为复数

14、ng-show/ng-hide 显示、隐藏元素

15、ng-style设置style  接收一个css对象  ng-style={{cssObj}}

16、ng-switch这个指令相当于通过$scope内部变量控制dom的隐藏和显示,其实这个指令非常强大, 相当于是为html代码提供了ifelse的功能 (因为angular的html中不能通过ifelse控制逻辑)

17、ng-transclude自定义标签

自定义指令

最后说明:指令是可以自定义的

具体的有4种表现方式:

  1. E 页面标签,也就是新的html元素<hello-angular></hello-angular>  
  2. A 元素的属性<input type="text" hello-angular="" />
  3. C css类选择器的值<input type="text"  class="hello-angular"/>
  4. M 注释<!-- director:hello-angular -->

先来创建一个自定义指令(13—23行代码)

 <!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>自定义指令demo</title>
</head>
<body>
<div ng-controller="testCon">
<hello-angular>beauty</hello-angular>
</div>
<script type="text/javascript" src="js/angular.js"></script>
<script>
angular.module('app',[])
.directive('helloAngular',function(){
return{
restrict:'AECM',
template:'<p>hello,<b ng-transclude></b></p>',
transclude:true
};
})
.controller('testCon',function($scope){ });
</script>
</body>
</html>

输出:    hello,beauty

没错就是上面那个输出,不信可以试试咯!

restrict:   翻译为”限制“,也许细心的人会发现这个属性,一一对应指令的四种变现方式,这就对了,这就是被允许的表现方式

templace:  这其中是标签也可以是字符串,不是标签的话会报错,它的兄弟属性是templateUrl,两者只需其一

templateUrl:  url?是的,必须是url才行咯,想想看,如果没有这个属性,template将会承载所有的标签,如果你要添加很长的一段代码,那么template的属性值会很长很长,

         肯定会不方便咯

transclude:    前面那个<b>标签的属性ng-transclude肯定和这个有关系对吧,设置为true,一个乾坤大挪移就把<hello-angular>移动到template中的<b>标签里面咯

接着,小小的改下代码,17 18行改成下面这样,看俺replace的作用

 restrict:'AECM',
replace:true,
template:'<p>hello</p>'

replace:    从单词的意思就可以看出来,替代,没错,就是用<p>hello</p>替代页面上的<hello-angular><hello-angular>

不要问卤煮为什么,自己敲一敲就ok。

再来一段代码,说明下面scope的用法

 <!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>自定义指令demo</title>
</head>
<body>
<div ng-controller="testCon">
<hello-angular info="person"></hello-angular>
</div>
<script type="text/javascript" src="js/angular.js"></script>
<script>
angular.module('app',[])
.directive('helloAngular',function(){
return{
restrict:'AECM',
scope:{
personReference:'=info'
},
template:'He is {{person.name}}'
};
})
.controller('testCon',function($scope){
$scope.person = {name:'Tom',age:180};
});
</script>
</body>
</html>

scope:自定义作用域,没有这个属性时默认使用父级controller的$scope,有socpe{}时,此scope继承父级controller的$scope

绑定策略的三种形式”=“,“@”,“&”   将本地作用域和DOM中的属性值绑定起来

”=“对一个对象的引用,后面的info是自定义标签中的属性(第9行代码),简而言之,personReference对象是person对象的引用

”@“表示对一个字符串的拷贝<ps:这儿不太确定,不过根据js来看,应该是这样>,这里不再赘述

”&“表示对父级作用域进行绑定,并将其中的属性包装成一个函数

 <!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>自定义指令demo</title>
</head>
<body>
<div ng-controller="testCon">
<hello-angular get-classes="classes" get-name="name"></hello-angular>
</div>
<script type="text/javascript" src="js/angular.js"></script>
<script>
angular.module('app',[])
.directive('helloAngular',function(){
return{
restrict:'AECM',
scope:{
getClasses:'&',
getName:'&'
},
template:'there are {{classes}} classes<br /><div ng-repeat="i in name">NO.{{$index+1}} className is {{i}}</div>',
controller:function($scope){
$scope.classes = $scope.getClasses();
$scope.name = $scope.getName();
}
};
})
.controller('testCon',function($scope){
$scope.classes = 3;
$scope.name = ['A','B','C'];
});
</script>
</body>
</html>

输出截图

“=”,“@”,“&”简写方式(代码第18,19行)scope{}中属性值可以简写为“@”,不过也必须遵循和DOM节点的属性一一对应的原则,angular会进行自动匹配

最新文章

  1. 《高性能javascript》一书要点和延伸(上)
  2. HDU 5726 GCD
  3. js正则函数
  4. 嵌入式 RTSP流媒体播放器实现
  5. linux 使用kill命令杀死进程的几个办法
  6. &#39;ascii&#39; codec can&#39;t encode characters in position 0-8: ordinal not in range(128)的解决办法
  7. ajax取json数据——简单的
  8. 将sql数据库逆向生成PDM模型
  9. Linux命令 rpm
  10. linux内核升级图文攻略
  11. 前端资讯周报 2.27 - 3.5: 如何设计一个优秀的HTML接口,深入理解line-height
  12. 使用docker安装mysql和redis
  13. SAP 图形页面
  14. SQLServer 查询数据死锁语句
  15. es6学习笔记10--箭头函数
  16. JAVA设计模式-设计原则
  17. 【laravel5.* + 钉钉实现WEB第三方登录】 使用redis 作为持久化存储
  18. Python 爬虫实例(11)—— 爬虫 苏宁易购
  19. MFC制作OCX
  20. vmware参数详解

热门文章

  1. HDU-2087 剪花布条
  2. 高效算法——C 分饼
  3. eucalyptus的网络模式
  4. 一个美国小券商的生存之道Tradestation
  5. 另一个有趣的Captcha 网站
  6. lucene4.5近实时搜索
  7. C# 对JS编码/解码进行转换
  8. jquery.scrollTo-min.js
  9. [c#]如何在form的webbrowser控件中获得鼠标坐标
  10. [转] 使用CodeViz生成C/C++函数调用关系图