angularJS directive详解

前言javascript

最近学习了下angularjs指令的相关知识,也参考了前人的一些文章,在此总结下。php

欢迎批评指出错误的地方。css

 

Angularjs指令定义的APIhtml

AngularJs的指令定义大体以下java

angular.module("app",[]).directive("directiveName",function(){     return{      //经过设置项来定义     }; })

其中return返回的对象包含不少参数,下面一一说明jquery

你知道用AngularJs怎么定义指令吗?0

1.restrictgit

(字符串)可选参数,指明指令在DOM里面以什么形式被声明;angularjs

取值有:E(元素),A(属性),C(类),M(注释),其中默认值为A;github

E(元素):<directiveName></directiveName>
A(属性):<div directiveName='expression'></div>
C(类):   <div class='directiveName'></div>
M(注释):<--directive:directiveName expression-->web

你知道用AngularJs怎么定义指令吗?1

例如restrict:‘EA’ 则表示指令在DOM里面可用元素形式和属性形式被声明;

通常来讲,当你建立一个有本身模板的组件的时候,须要使用元素名,若是仅仅是为为已有元素添加功能的话,就使用属性名

注意:若是想支持IE8,则最好使用属性和类形式来定义。 另外Angular从1.3.x开始, 已经放弃支持IE8了.

2.priority

(数字),可选参数,指明指令的优先级,若在单个DOM上有多个指令,则优先级高的先执行;

设置指令的优先级算是不经常使用的

比较特殊的的例子是,angularjs内置指令的ng-repeat的优先级为1000,ng-init的优先级为450;

3.terminal

(布尔型),可选参数,能够被设置为true或false,若设置为true,则优先级低于此指令的其余指令则无效,不会被调用(优先级相同的仍是会执行)

4.template(字符串或者函数)可选参数,能够是:

(1)一段HTML文本

angular.module("app",[]).directive("hello",function(){                 return{                  restrict:'EA',                  template:"<div><h3>hello world</h3></div>"                 };             })
HTML代码为:<hello></hello>
结果渲染后的HTML为:<hello>    <div><h3>hello world</h3></div> </hello>

(2)一个函数,可接受两个参数tElement和tAttrs

其中tElement是指使用此指令的元素,而tAttrs则实例的属性,它是一个由元素上全部的属性组成的集合(对象)形如:

{
title:‘aaaa’,
name:'leifeng' }

下面让咱们看看template是一个函数时候的状况

angular.module("app",[]).directive("directitle",function(){                 return{                  restrict:'EAC',                  template: function(tElement,tAttrs){                     var _html = '';                     _html += '<div>'+tAttrs.title+'</div>';                     return _html;                  }                 };             })
HTML代码:<directitle title='biaoti'></directitle>
渲染以后的HTML:<div>biaoti</div>

由于一段HTML文本,阅读跟维护起来都是很麻烦的,所用一般会使用templateUrl这个。

5.templateUrl(字符串或者函数),可选参数,能够是

(1)一个表明HTML文件路径的字符串

(2)一个函数,可接受两个参数tElement和tAttrs(大体同上)

注意:在本地开发时候,须要运行一个服务器,否则使用templateUrl会报错 Cross Origin Request Script(CORS)错误

因为加载html模板是经过异步加载的,若加载大量的模板会拖慢网站的速度,这里有个技巧,就是先缓存模板

你能够再你的index页面加载好的,将下列代码做为你页面的一部分包含在里面。

<script type='text/ng-template' id='woshimuban.html'>            <div>我是模板内容</div> </script>

这里的id属性就是被设置在templateUrl上用的。

另外一种办法缓存是:

angular.module("template.html", []).run(["$templateCache", function($templateCache) {   $templateCache.put("template.html",     "<div>wo shi mu ban</div>"); }]);

 

 6.replace

(布尔值),默认值为false,设置为true时候,咱们再来看看下面的例子(对比下在template时候举的例子)

angular.module("app",[]).directive("hello",function(){                 return{                  restrict:'EA',                  replace:true,                  template:"<div><h3>hello world</h3></div>"                 };             })
HTML代码为:
<hello></hello>
渲染以后的代码:<div><h3>hello world</h3></div>

对比下没有开启replace时候的渲染出来的HTML。发现<hello></hello>不见了。

另外当模板为纯文本(即template:"wo shi wen ben")的时候,渲染以后的html代码默认的为文本用span包含。

7.scope

可选参数,(布尔值或者对象)默认值为false,可能取值:

(1)默认值false。

表示继承父做用域;

(2)true

表示继承父做用域,并建立本身的做用域(子做用域);

(3){}

表示建立一个全新的隔离做用域;

7.1首先咱们先来了解下scope的继承机制。咱们用ng-controller这个指令举例,

咱们都知道ng-controller(内置指令)能够从父做用域中继承而且建立一个新的子做用域。以下:

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>   <div ng-init="aaa='父亲'">     parentNode:{{aaa}}     <div ng-controller="myController">         chrildNode: {{aaa}}     </div>   </div>   <script>     angular.module('myApp', [])     .controller('myController',function($scope){        $scope.aaa = '儿子'     })   </script> </body> </html>

这时页面显示是

parentNode:父亲

chrildNode: 儿子

若去掉

 $scope.aaa = '儿子'

则显示

parentNode:父亲

chrildNode: 父亲

注意:

1)若一个元素上有多个指令,使用了隔离做用域,则只有其中一个能够生效;

2)只有指令模板中的根元素才能得到一个新的做用域,这时候,scope就被设置为true了;

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>   <div ng-init="aaa='父亲'">     parentNode:{{aaa}}     <div class='one' ng-controller="myController">         chrildNode: {{aaa}}         <div class='two' ng-controller='myController2'>           {{aaa}}         </div>     </div>   </div>   <script>     angular.module('myApp', [])     .controller('myController',function($scope){       $scope.aaa = '儿子';     })     .controller('myController2',function($scope){       $scope.aaa = '孙女';     })   </script> </body> </html>

页面显示为:

parentNode:父亲

chrildNode: cunjieliu

孙女

上面中class为one那个div得到了指令ng-controller=’myController‘所建立的新的做用域;

而class为two那个div得到了指令ng-controller=’myController2‘所建立的新的做用域;

这就是“只有指令模板中的根元素才能得到一个新的做用域”;

 

接下来咱们经过一个简单明了的例子来讲明scope取值不一样的差异

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>   <div ng-controller='MainController'>         父亲: {{name}}         <input ng-model="name" />         <div my-directive></div>   </div>   <script>     angular.module('myApp', [])         .controller('MainController'function ($scope) {            $scope.name = 'leifeng';         })         .directive('myDirective'function () {             return {                 restrict: 'EA',                 scope:false,//改变此处的取值,看看有什么不一样                 template: '<div>儿子:{{ name }}<input ng-model="name"/></div>'             };         });   </script> </body> </html>

依次设置scope的值false,true,{},结果发现(你们别偷懒,动手试试哈)

当为false时候,儿子继承父亲的值,改变父亲的值,儿子的值也随之变化,反之亦如此。(继承不隔离)

当为true时候,儿子继承父亲的值,改变父亲的值,儿子的值随之变化,可是改变儿子的值,父亲的值不变。(继承隔离)

当为{}时候,没有继承父亲的值,因此儿子的值为空,改变任何一方的值均不能影响另外一方的值。(不继承隔离)

tip:当你想要建立一个可重用的组件时隔离做用域是一个很好的选择,经过隔离做用域咱们确保指令是‘独立’的,并能够轻松地插入到任何HTML app中,而且这种作法防止了父做用域被污染;

7.2隔离做用域能够经过绑定策略来访问父做用域的属性。

下面看一个例子

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>   <div ng-controller='MainController'>         <input type="text" ng-model="color" placeholder="Enter a color"/>            <hello-world></hello-world>   </div>   <script>     var app = angular.module('myApp',[]);     app.controller('MainController',function(){});     app.directive('helloWorld',function(){      return {         scope: false,         restrict: 'AE',         replace: true,         template: '<p style="Hello World</p>'            }     });   </script> </body> </html>

运行代码,并在input中输入颜色值,结果为

你知道用AngularJs怎么定义指令吗?2

可是,但咱们将scope设置为{}时候,再次运行上面的代码能够发现页面并不能成功完整显示!

缘由在于,这里咱们将scope设置为{},产生了隔离做用域。

因此在template模板中{{color}}变成了依赖于本身的做用域,而不是依赖于父做用域。

所以咱们须要一些办法来让隔离做用域能读取父做用域的属性,就是绑定策略。

下面咱们就来探索设置这种绑定的几种方法

方法一:使用@(@attr)来进行单向文本(字符串)绑定

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>   <div ng-controller='MainController'>         <input type="text" ng-model="color" placeholder="Enter a color"/>            <hello-world color-attr='{{color}}'></hello-world>   //注意这里设置了color-attr属性,绑定了{{color}}   </div>   <script>     var app = angular.module('myApp',[]);     app.controller('MainController',function(){});     app.directive('helloWorld',function(){      return {         scope: {color:'@colorAttr'},  //指明了隔离做用域中的属性color应该绑定到属性colorAttr         restrict: 'AE',         replace: true,         template: '<p style=" -webkit-tap-highlight-color: transparent; color: rgb(239, 220, 188);">{{color}}">Hello World</p>'            }     });   </script> </body> </html>

这种办法只能单向,经过在运行的指令的那个html标签上设置color-attr属性,而且采用{{}}绑定某个模型值。

注意,你也能够再这里直接绑定字符串的颜色值,如:color-attr=“red”;

而后你能够看到表达式{{color}}被赋值给了color-attr。

当表达式的值发生变化时,属性color-attr也会发生变化,因此也改变了隔离做用域中的属性color。

tips:若是绑定的隔离做用域属性名与元素的属性名相同,则能够采起缺省写法。

html:
<hello-world color="{{color}}"/>   js定义指令的片断: app.directive('helloWorld',function(){     return {         scope: {             color: '@'         },         ...         //配置的余下部分     } });

方法二:使用=(=attr)进行双向绑定

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>   <div ng-controller='MainController'>         <input type="text" ng-model="color" placeholder="Enter a color"/>         {{color}}         <hello-world color='color'></hello-world>  //注意这里的写法   </div>   <script>     var app = angular.module('myApp',[]);     app.controller('MainController',function(){});     app.directive('helloWorld',function(){      return {         scope:{color:'='},         restrict: 'AE',         replace: true,         template: '<div style=" -webkit-tap-highlight-color: transparent; color: rgb(239, 220, 188);">{{color}}">Hello World<div><input type="text" ng-model="color"></div></div>'            }     });   </script> </body> </html>

此处也相似上面采用了缺省的写法。

这里须要注意的是,咱们要直接在指令运行的那个元素上设置属性时候,是直接将 实际的做用域模型 赋值给该属性(这里就是color)

这样一个双向绑定被创建了,改变任何一个input都会改变另外一个值。

你知道用AngularJs怎么定义指令吗?3

方法三:使用&来调用父做用域中的函数

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>   <div ng-controller='MainController'>         <input type="text" ng-model="name" placeholder="Enter a color"/>         {{name}}         <hello-world saysomething999="say();" name="liucunjie"></hello-world> //注意这里   </div>   <script>     var app = angular.module('myApp',[]);     app.controller('MainController',function($scope){       $scope.say = function(){         alert('hello');       }       $scope.name = 'leifeng';     });     app.directive('helloWorld',function(){      return {         scope:{           saysomething:'&saysomething999',           name:'@'         },         restrict: 'AE',         replace: true,         template: '<button type="button" ng-bind="name" ng-init="saysomething();"></button>'      }     });   </script> </body> </html>

运行以后,弹出alert框。

8.transclude

(布尔值或者字符‘element’),默认值为false;

这个配置选项可让咱们提取包含在指令那个元素里面的内容,再将它放置在指令模板的特定位置。

当你开启transclude后,你就可使用ng-transclude来指明了应该在什么地方放置transcluded内容

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>   <div ng-controller='MainController'>         <div class='a'>           <p>china</p>           <hello-world>             {{name}}           </hello-world>         </div>   </div>   <script>     var app = angular.module('myApp',[]);     app.controller('MainController',function($scope){       $scope.name = 'leifeng';     });     app.directive('helloWorld',function(){      return {         scope:{},           restrict: 'AE',         transclude: true,         template: '<div class="b"><div ng-transclude>你看不见我</div></div>'      }     });   </script> </body> </html>

运行上面的代码,输出

china

leifeng

另外当开启transclude,会建立一个新的transclude空间,而且继承了父做用域(即便Scope设置为隔离做用域),

上面代码中的{{name}}是依赖于父做用域的,仍然能被渲染出来,就说明了这点。

咱们再看看生成的html为下图所示,能够发现文本“你看不见我”消失了,这是由于被transclude内容替换掉了。

这里的transclude内容就是{{name}}

你知道用AngularJs怎么定义指令吗?4

接下来再来看看transclude的另外一个取值transclude:“element”

那transclude:“element”与transclude:true有什么区别呢?

区别在于嵌入的内容,以上面的例子来讲,

当transclude:true时候,嵌入的内容为{{name}},

而当transclude:“element”时候,嵌入的内容为

<hello-world>    {{name}} </hello-world>

没错,此时嵌入的内容为整个元素。

将上面代码transclude:true换成transclude:true后,再运行,你会发现结果并非和你想的同样

再次查看生成的html代码

你知道用AngularJs怎么定义指令吗?5

你会发现指令绑定的元素被转换为了一个HTML注释

关于这方面的疑问能够查看 transclude: 'element' is useless without replace:true 获取更多

解决方案是加上replace: true,就正常了

这时再查看HTML代码

你知道用AngularJs怎么定义指令吗?6

 注意:在一个指令的模板template上只能申明一个ng-transclude。

OK,那么如今问题来了,若是咱们想把嵌入部分屡次放入咱们的模板中要怎么办?

则可使用$transclude(后面再controller选项中会讲)

或者可使用compile函数,里面有个transcludeFn参数(后面会讲)

或者使用link连接函数。。。

9.controller

能够是一个字符串或者函数。

如果为字符串,则将字符串当作是控制器的名字,来查找注册在应用中的控制器的构造函数

angular.module('myApp', [])  .directive('myDirective', function() {  restrict: 'A', // 始终须要 controller: 'SomeController'  })  // 应用中其余的地方,能够是同一个文件或被index.html包含的另外一个文件 angular.module('myApp')  .controller('SomeController', function($scope, $element, $attrs, $transclude) {  // 控制器逻辑放在这里 });

也能够直接在指令内部的定义为匿名函数,一样咱们能够再这里注入任何服务($log,$timeout等等)

angular.module('myApp',[])  .directive('myDirective', function() {  restrict: 'A',  controller:  function($scope, $element, $attrs, $transclude) {  // 控制器逻辑放在这里 }  });

另外还有一些特殊的服务(参数)能够注入

(1)$scope,与指令元素相关联的做用域

(2)$element,当前指令对应的 元素

(3)$attrs,由当前元素的属性组成的对象

(4)$transclude,嵌入连接函数,实际被执行用来克隆元素和操做DOM的函数

注意: 除非是用来定义一些可复用的行为,通常不推荐在这使用。

         指令的控制器和link函数(后面会讲)能够进行互换。区别在于,控制器主要是用来提供可在指令间复用的行为但link连接函数只能在当前内部指令中定义行为,且没法再指令间复用。

html代码:   <my-site site="http://www.cnblogs.com/cunjieliu">雷锋叔叔的博客</my-site>             js代码:   <script>     angular.module('myApp',[]).directive('mySite', function () {      return {          restrict: 'EA',          transclude: true, //注意此处必须设置为true          controller:          function ($scope, $element,$attrs,$transclude,$log) {  //在这里你能够注入你想注入的服务              $transclude(function (clone) {                                var a = angular.element('<a>');                  a.attr('href', $attrs.site);                  a.text(clone.text());                  $element.append(a);              });              $log.info("hello everyone");          }      };  });   </script>

运行上面的代码就是

你知道用AngularJs怎么定义指令吗?7

而且在控制台下输出hello everyone

让咱们看看$transclude();在这里,它能够接收两个参数,第一个是$scope,做用域,第二个是带有参数clone的回调函数。

而这个clone实际上就是嵌入的内容(通过jquery包装),能够在它上作不少DOM操做。

它还有最简单的用法就是

<script>
    angular.module('myApp',[]).directive('mySite', function () {      return {          restrict: 'EA',          transclude: true,          controller:          function ($scope, $element,$attrs,$transclude,$log) {              var a = $transclude(); //$transclude()就是嵌入的内容              $element.append(a);          }      };  });  </script>

注意:使用$transclude会生成一个新的做用域。

默认状况下,若是咱们简单实用$transclude(),那么默认的其做用域就是$transclude生成的做用域

可是若是咱们实用$transclude($scope,function(clone){}),那么做用域就是directive的做用域了

那么问题又来了。若是咱们想实用父做用域呢

可使用$scope.$parent

<div ng-controller='parentctrl'>         <div ng-controller='sonctrl'>           <my-site site="http://www.cnblogs.com/cunjieliu"><div>雷锋叔叔的博客</div></my-site>         </div>   </div>    <script>     var app = angular.module('myApp',[]);     app.controller('sonctrl',function($scope){       $scope.title = 'hello son';     });     app.controller('parentctrl',function($scope){       $scope.title = 'hello parent';     });     app.directive('mySite', function () {      return {          restrict: 'EA',          transclude: true,          controller:          function ($scope, $element,$attrs,$transclude,$log) {             var a = $transclude();             $element.append(a);             $log.info($scope.title);             $log.info($scope.$parent.title);                   }      };  });   </script>

同理想要一个新的做用域也可使用$scope.$parent.new();

10.controllerAs

这个选项的做用是能够设置你的控制器的别名

通常之前咱们常常用这样方式来写代码:

angular.module("app",[])   .controller("demoController",["$scope",function($scope){     $scope.title = "angualr";   }])  <div ng-app="app" ng-controller="demoController">       {{title}} </div>

后来angularjs1.2给咱们带来新语法糖,因此咱们能够这样写

angular.module("app",[])   .controller("demoController",[function(){     this.title = "angualr";   }])   <div ng-app="app" ng-controller="demoController as demo">        {{demo.title}}   </div>

一样的咱们也能够再指令里面也这样写

<script>     angular.module('myApp',[]).directive('mySite'function () {      return {          restrict: 'EA',          transclude: true,          controller:'someController',          controllerAs:'mainController'          //..其余配置      };  });  </script>

11.require(字符串或者数组)

字符串表明另外一个指令的名字,它将会做为link函数的第四个参数

具体用法咱们能够举个例子说明

假设如今咱们要编写两个指令,两个指令中的link连接函数中(link函数后面会讲)存在有不少重合的方法,

这时候咱们就能够将这些重复的方法写在第三个指令的controller中(上面也讲到controller常常用来提供指令间的复用行为)

而后在这两个指令中,require这个拥有controller字段的的指令(第三个指令),

最后经过link连接函数的第四个参数就能够引用这些重合的方法了。

<!doctype html> <html ng-app="myApp"> <head>   <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body>               <outer-directive>      <inner-directive></inner-directive>      <inner-directive2></inner-directive2>   </outer-directive>   <script>     var app = angular.module('myApp', []);     app.directive('outerDirective'function({           return {                scope: {},                restrict: 'AE',                controller: function($scope{                         this.say = function(someDirective{                       console.log('Got:' + someDirective.message);                   };                }            };     });     app.directive('innerDirective'function({           return {                scope: {},                restrict: 'AE',                require'^outerDirective',                link: function(scope, elem, attrs, controllerInstance{                        scope.message = "Hi,leifeng";                        controllerInstance.say(scope);                }           };     });     app.directive('innerDirective2'function({           return {                scope: {},                restrict: 'AE',                require'^outerDirective',                link: function(scope, elem, attrs, controllerInstance{                        scope.message = "Hi,shushu";                        controllerInstance.say(scope);                }           };     });             </script> </body> </html>

上面例子中的指令innerDirective和指令innerDirective2复用了定义在指令outerDirective的controller中的方法

也进一步说明了,指令中的controller是用来让不一样指令间通讯用的。

另外咱们能够在require的参数值加上下面的某个前缀,这会改变查找控制器的行为:

(1)没有前缀,指令会在自身提供的控制器中进行查找,若是找不到任何控制器,则会抛出一个error

(2)?若是在当前的指令没有找到所需的控制器,则会将null传给link链接函数的第四个参数

(3)^若是在当前的指令没有找到所需的控制器,则会查找父元素的控制器

(4)?^组合

12.Anguar的指令编译过程

首先加载angularjs库,查找到ng-app指令,从而找到应用的边界,

根据ng-app划定的做用域来调用$compile服务进行编译,

angularjs会遍历整个HTML文档,并根据js中指令的定义来处理在页面上声明的各个指令

按照指令的优先级(priority)排列,根据指令中的配置参数(template,place,transclude等)转换DOM

而后就开始按顺序执行各指令的compile函数(若是指令上有定义compile函数)对模板自身进行转换

注意:此处的compile函数是咱们指令中配置的,跟上面说的$compile服务不同。

每一个compile函数执行完后都会返回一个link函数,全部的link函数会合成一个大的link函数

而后这个大的link函数就会被执行,主要作数据绑定,经过在DOM上注册监听器来动态修改scope中的数据,

或者是使用$watchs监听 scope中的变量来修改DOM,从而创建双向绑定等等。

若咱们的指令中没有配置compile函数,那咱们配置的link函数就会运行,

她作的事情大体跟上面complie返回以后全部的link函数合成的的大的link函数差很少。

因此:在指令中compile与link选项是互斥的,若是同时设置了这两个选项,

         那么就会把compile所返回的函数当作是连接函数,而link选项自己就会被忽略掉

 

13.compile编译函数和link连接函数

13.1compile编译函数选项

compile选项能够返回一个对象或者函数

在这里咱们能够在指令和实时数据被放到DOM中以前进行DOM操做,

好比咱们能够在这里进行添加或者删除节点的DOM的操做。

因此编译函数是负责对模板的DOM进行转换,而且仅仅只会运行一次。

//compile函数的语法 compile:function compile(tElement,tAttrs,transclude){       return{         pre:function preLink(scope,iElement,iAttrs,controller){},         post:function postLink(scope,iElement,iAttrs,controller){}       }     }

对于咱们编写的大部分的指令来讲,并不须要对模板进行转换,因此大部分状况只要编写link函数就能够了。

tips:preLink函数会在编译阶段以后,指令连接到子元素以前执行

        相似的,postLink会在指令连接到子元素以后执行

        这意味着,为了避免破坏绑定过程,若是你须要修改DOM结构,你应该在postLink函数中来作这件事。

13.2link连接函数选项

连接函数负责将做用域和DOM进行连接。

//link连接函数 link:function postLink(scope,iElement,iAttrs){}

若指令中定义有require选项,则link函数会有第四个参数,表明控制器或者所依赖的指令的控制器(上面require选项例子已有例子)

14.ngModel

 偷个懒,这个重要的东西就留着下次再讲吧。。。

相关文章
相关标签/搜索