天天看点

angularjs directive 实例 详解 angularjs directive 实例 详解                                               张映 发表于 2014-03-13     

angularjs directive 实例 详解

                                              张映 发表于 2014-03-13     

前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的model,也可以理解成插件,只不过这种model是带html的,例如:php的分页函数。

一,angularjs directive的常用格式,以及参数说明

1,直接return

  1. var phonecatDirectives = angular.module('phonecatDirectives', []);  
  2. phonecatDirectives.directive('directiveName', function($inject) {  
  3.    return {  
  4.      template: '<div></div>',  
  5.      replace: false,  
  6.      transclude: true,  
  7.      restrict: 'E',  
  8.      scope: { ... },  
  9.       controller: function($scope, $element){ .... },     
  10.       compile: function(tElement, tAttrs, transclude) {  
  11.        return {  
  12.          pre: function preLink(scope, iElement, iAttrs, controller) { ... },  
  13.          post: function postLink(scope, iElement, iAttrs, controller) { ... }  
  14.        }  
  15.      },  
  16.      link: function(scope, iElement, iAttrs) { ... }  
  17.    };  
  18. });  

2,定义一个js的域

  1. var phonecatDirectives = angular.module('phonecatDirectives', []);  
  2. phonecatDirectives.directive('directiveName', function($inject) {  
  3.    var mydi = {  
  4.      template: '<div></div>',  
  5.       ..................... ,  
  6.      link: function(scope, iElement, iAttrs) { ... }  
  7.    };  
  8.    return mydi;  
  9. });  

3,angularjs directive 对像参数说明

name - 当前scope的名称,注册时可以使用默认值(不填)。

priority(优先级)- 当有多个directive定义在同一个DOM元素时,有时需要明确它们的执行顺序。这属性用于在directive的compile function调用之前进行排序。如果优先级相同,则执行顺序是不确定的(经初步试验,优先级高的先执行,同级时按照类似栈的“后绑定先执行”。另外,测试时有点不小心,在定义directive的时候,两次定义了一个相同名称的directive,但执行结果发现,两个compile或者link function都会执行)。

terminal(最后一组)- 如果设置为”true”,则表示当前的priority将会成为最后一组执行的directive。任何directive与当前的优先级相同的话,他们依然会执行,但顺序是不确定的(虽然顺序不确定,但基本上与priority的顺序一致。当前优先级执行完毕后,更低优先级的将不会再执行)。

scope - 如果设置为:

true - 将为这个directive创建一个新的scope。如果在同一个元素中有多个directive需要新的scope的话,它还是只会创建一个scope。新的作用域规则不适用于根模版(root of the template),因此根模版往往会获得一个新的scope。

{}(object hash) - 将创建一个新的、独立(isolate)的scope。”isolate” scope与一般的scope的区别在于它不是通过原型继承于父scope的。这对于创建可复用的组件是很有帮助的,可以有效防止读取或者修改父级scope的数据。这个独立的scope会创建一个拥有一组来源于父scope的本地scope属性(local scope properties)的object hash。这些local properties对于为模版创建值的别名很有帮助(useful for aliasing values for templates -_-!)。本地的定义是对其来源的一组本地scope property的hash映射(Locals definition is a hash of local scope property to its source #&)$&@#)($&@#_):

@或@attr - 建立一个local scope property到DOM属性的绑定。因为属性值总是String类型,所以这个值总是返回一个字符串。如果没有通过@attr指定属性名称,那么本地名称将与DOM属性的名称一直。例如<widget my-attr=”hello {{name}}”>,widget的scope定义为:{localName:’@myAttr’}。那么,widget scope property的localName会映射出”hello {{name}}"转换后的真实值。name属性值改变后,widget scope的localName属性也会相应地改变(仅仅单向,与下面的”=”不同)。name属性是在父scope读取的(不是组件scope)

=或=expression(这里也许是attr) - 在本地scope属性与parent scope属性之间设置双向的绑定。如果没有指定attr名称,那么本地名称将与属性名称一致。例如<widget my-attr=”parentModel”>,widget定义的scope为:{localModel:’=myAttr’},那么widget scope property “localName”将会映射父scope的“parentModel”。如果parentModel发生任何改变,localModel也会发生改变,反之亦然。(双向绑定)

&或&attr - 提供一个在父scope上下文中执行一个表达式的途径。如果没有指定attr的名称,那么local name将与属性名称一致。例如<widget my-attr=”count = count + value”>,widget的scope定义为:{localFn:’increment()’},那么isolate scope property “localFn”会指向一个包裹着increment()表达式的function。一般来说,我们希望通过一个表达式,将数据从isolate scope传到parent scope中。这可以通过传送一个本地变量键值的映射到表达式的wrapper函数中来完成。例如,如果表达式是increment(amount),那么我们可以通过localFn({amount:22})的方式调用localFn以指定amount的值(上面的例子真的没看懂,&跑哪去了?)。

controller - controller 构造函数。controller会在pre-linking步骤之前进行初始化,并允许其他directive通过指定名称的require进行共享(看下面的require属性)。这将允许directive之间相互沟通,增强相互之间的行为。controller默认注入了以下本地对象:

$scope - 与当前元素结合的scope

$element - 当前的元素

$attrs - 当前元素的属性对象

$transclude - 一个预先绑定到当前转置scope的转置linking function :function(cloneLinkingFn)。(A transclude linking function pre-bound to the correct transclusion scope)

require - 请求另外的controller,传入当前directive的linking function中。require需要传入一个directive controller的名称。如果找不到这个名称对应的controller,那么将会抛出一个error。名称可以加入以下前缀:

? - 不要抛出异常。这使这个依赖变为一个可选项。

^ - 允许查找父元素的controller

restrict - EACM的子集的字符串,它限制directive为指定的声明方式。如果省略的话,directive将仅仅允许通过属性声明:

E - 元素名称: <my-directive></my-directive>

A - 属性名: <div my-directive=”exp”></div>

C - class名: <div class=”my-directive:exp;”></div>

M - 注释 : <!-- directive: my-directive exp -->

template - 如果replace 为true,则将模版内容替换当前的HTML元素,并将原来元素的属性、class一并迁移;如果为false,则将模版元素当作当前元素的子元素处理。想了解更多的话,请查看“Creating Widgets”章节(在哪啊。。。Creating Components就有。。。)

templateUrl - 与template基本一致,但模版通过指定的url进行加载。因为模版加载是异步的,所以compilation、linking都会暂停,等待加载完毕后再执行。

replace - 如果设置为true,那么模版将会替换当前元素,而不是作为子元素添加到当前元素中。(注:为true时,模版必须有一个根节点)

transclude - 编译元素的内容,使它能够被directive所用。需要(在模版中)配合ngTransclude使用(引用)。transclusion的优点是linking function能够得到一个预先与当前scope绑定的transclusion function。一般地,建立一个widget,创建isolate scope,transclusion不是子级的,而是isolate scope的兄弟。这将使得widget拥有私有的状态,transclusion会被绑定到父级(pre-isolate)scope中。(上面那段话没看懂。但实际实验中,如果通过<any my-directive>{{name}}</any my-directive>调用myDirective,而transclude设置为true或者字符串且template中包含<sometag ng-transclude>的时候,将会将{{name}}的编译结果插入到sometag的内容中。如果any的内容没有被标签包裹,那么结果sometag中将会多了一个span。如果本来有其他东西包裹的话,将维持原状。但如果transclude设置为’element’的话,any的整体内容会出现在sometag中,且被p包裹)

true - 转换这个directive的内容。(这个感觉上,是直接将内容编译后搬入指定地方)

‘element’ - 转换整个元素,包括其他优先级较低的directive。(像将整体内容编译后,当作一个整体(外面再包裹p),插入到指定地方)

compile - 这里是compile function,将在下面实例详细讲解

link - 这里是link function ,将在下面实例详细讲解。这个属性仅仅是在compile属性没有定义的情况下使用。

三,angularjs directive 实例讲解

下面的实例都围绕着,上面的参数来展开的

1,directive声明方式实例

  1. //directive文件directives.js中定义一个myTest  
  2. 'use strict';  
  3. var phonecatDirectives = angular.module('phonecatDirectives', []);  
  4. phonecatDirectives.directive('myTest', function() {  
  5.     return {  
  6.         restrict: 'ACEM',  
  7.         require: '^ngModel',  
  8.         scope: {  
  9.             ngModel: '='  
  10.         },  
  11.         template: '<div><h4>Weather for {{ngModel}}</h4></div>'  
  12.     }  
  13. });  
  14. //controller文件controller.js中定义directive1  
  15. 'use strict';  
  16. var dtControllers = angular.module('dtControllers', []);  
  17. dtControllers.controller('directive1',['$scope',  
  18.     function($scope) {  
  19.         $scope.name = 'this is tank test';  
  20.     }  
  21. ]);  
  22. //在app文件app.js中整合controller,directive  
  23. 'use strict';  
  24. var phonecatApp = angular.module('phonecatApp', [  
  25.     'phonecatDirectives',  
  26.     'dtControllers'  
  27. ]);  
  28. //html文件  
  29. <script src="../lib/angular/angular.js"></script>  
  30. <script src="../js/app.js"></script>  
  31. <script src="../js/controller.js"></script>  
  32. <script src="../js/directives.js"></script>  
  33. <body ng-app="phonecatApp">  
  34.     <div ng-controller="directive1">  
  35.         <input type="text" ng-model="city" placeholder="Enter a city" />  
  36.         <my-test ng-model="city" ></my-test>  
  37.         <span my-test="exp" ng-model="city"></span>  
  38.         <!-- directive: my-test exp -->  
  39.         <span ng-model="city"></span>  
  40.     </div>  
  41. </body>  

上例结果:<!-- directive: my-test exp -->这个不起作用,不知道为什么,尝试了好多方法都不起作用。

2,template和templateUrl区别与联系

templateUrl其实根template功能是一样的,只不过templateUrl加载一个html文件,上例中,我们也能发现问题,template后面根的是html的标签,如果标签很多呢,那就比较不爽了。可以将上例中的,template改一下。

  1. phonecatDirectives.directive('myTest', function() {  
  2.     return {  
  3.         restrict: 'ACEM',  
  4.         require: '^ngModel',  
  5.         scope: {  
  6.             ngModel: '='  
  7.         },  
  8.         templateUrl:'../partials/tem1.html'   //tem1.html中的内容就是上例中template的内容。  
  9.     }  
  10. });  

3,scope重定义

  1. //directives.js中定义myAttr  
  2. phonecatDirectives.directive('myAttr', function() {  
  3.     return {  
  4.         restrict: 'E',  
  5.         scope: {  
  6.             customerInfo: '=info'  
  7.         },  
  8.         template: 'Name: {{customerInfo.name}} Address: {{customerInfo.address}}<br>' +  
  9.                   'Name: {{vojta.name}} Address: {{vojta.address}}'  
  10.     };  
  11. });  
  12. //controller.js中定义attrtest  
  13. dtControllers.controller('attrtest',['$scope',  
  14.     function($scope) {  
  15.         $scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };  
  16.         $scope.vojta = { name: 'Vojta', address: '3456 Somewhere Else' };  
  17.     }  
  18. ]);  
  19. //html中  
  20. <body ng-app="phonecatApp">  
  21.     <div ng-controller="attrtest">  
  22.         <my-attr info="naomi"></my-attr>  
  23.     </div>  
  24. </body>  

运行结果:

  1. Name: Naomi Address: 1600 Amphitheatre      //有值,因为customerInfo定义过的  
  2. Name: Address:       //没值 ,因为scope重定义后,vojta是没有定义的  

可能把上面的directive简单改一下,

  1. phonecatDirectives.directive('myAttr', function() {  
  2.     return {  
  3.         restrict: 'E',  
  4.         template: 'Name: {{customerInfo.name}} Address: {{customerInfo.address}}<br>' +  
  5.                   'Name: {{vojta.name}} Address: {{vojta.address}}'  
  6.     };  
  7. });  

运行结果如下:根上面正好相反

  1. Name: Address:  
  2. Name: Vojta Address: 3456 Somewhere Else  

4,transclude的使用

transclude的用法,有点像jquery里面的$().html()功能

  1. //directives.js增加myEvent  
  2. phonecatDirectives.directive('myEvent', function() {  
  3.     return {  
  4.         restrict: 'E',  
  5.         transclude: true,  
  6.         scope: {  
  7.             'close': '&onClick'     //根html中的on-click="hideDialog()"有关联关系  
  8.         },  
  9.         templateUrl: '../partials/event_part.html'  
  10.     };  
  11. });  
  12. //controller.js增加eventtest  
  13. dtControllers.controller('eventtest',['$scope','$timeout',  
  14.     function($scope, $timeout) {  
  15.         $scope.name = 'Tobias';  
  16.         $scope.hideDialog = function () {  
  17.             $scope.dialogIsHidden = true;  
  18.             $timeout(function () {  
  19.                 $scope.dialogIsHidden = false;  
  20.             }, 2000);  
  21.         };  
  22.     }  
  23. ]);  
  24. //event.html  
  25. <body ng-app="phonecatApp">  
  26.     <div ng-controller="eventtest">  
  27.         <my-event ng-hide="dialogIsHidden" on-click="hideDialog()">  
  28.             Check out the contents, {{name}}!  
  29.         </my-event>  
  30.     </div>  
  31. </body>  
  32. //event_part.html  
  33. <div>  
  34.     <a href ng-click="close()">×</a>  
  35.     <div ng-transclude></div>  
  36. </div>  

5,controller,link,compile有什么不同

  1. //directives.js增加exampleDirective  
  2. phonecatDirectives.directive('exampleDirective', function() {  
  3.     return {  
  4.         restrict: 'E',  
  5.         template: '<p>Hello {{number}}!</p>',  
  6.         controller: function($scope, $element){  
  7.             $scope.number = $scope.number + "22222 ";  
  8.         },  
  9.         link: function(scope, el, attr) {  
  10.             scope.number = scope.number + "33333 ";  
  11.         },  
  12.         compile: function(element, attributes) {  
  13.             return {  
  14.                 pre: function preLink(scope, element, attributes) {  
  15.                     scope.number = scope.number + "44444 ";  
  16.                 },  
  17.                 post: function postLink(scope, element, attributes) {  
  18.                     scope.number = scope.number + "55555 ";  
  19.                 }  
  20.             };  
  21.         }  
  22.     }  
  23. });  
  24. //controller.js添加  
  25. dtControllers.controller('directive2',['$scope',  
  26.     function($scope) {  
  27.         $scope.number = '1111 ';  
  28.     }  
  29. ]);  
  30. //html  
  31. <body ng-app="phonecatApp">  
  32.     <div ng-controller="directive2">  
  33.         <example-directive></example-directive>  
  34.     </div>  
  35. </body>  

运行结果:

  1. Hello 1111 22222 44444 55555 !  

由结果可以看出来,controller先运行,compile后运行,link不运行。

将上例中的compile注释掉

  1. //        compile: function(element, attributes) {  
  2. //            return {  
  3. //                pre: function preLink(scope, element, attributes) {  
  4. //                    scope.number = scope.number + "44444 ";  
  5. //                },  
  6. //                post: function postLink(scope, element, attributes) {  
  7. //                    scope.number = scope.number + "55555 ";  
  8. //                }  
  9. //            };  
  10. //        }  

运行结果:

  1. Hello 1111 22222 33333 !  

由结果可以看出来,controller先运行,link后运行,link和compile不兼容。

作者:海底苍鹰

地址:http://blog.51yip.com/jsjquery/1607.html

=============================

在angular中我们定义directive方法时,可以看到

[html]  view plain copy

  1. return {  
  2.     restrict: 'AE',  
  3.     scope: {},  
  4.     template: '<div></div>',  
  5.     link: function() {}  
  6. }  

除了代码中出现的属性,还有一些其他的属性可供配置,这里不作详述。

今天我们要说的重点是scope字段。

常规用法设置

[javascript]  view plain copy

  1. scope: {  
  2.     name: '=',  
  3.     age: '=',  
  4.     sex: '@',  
  5.     say: '&'  
  6. }  

假设我们的hml代码如下

[html]  view plain copy

  1. <div my-directive name="myName" age="myAge" sex="male" say="say()"></div>  

对应的controller部分代码

[javascript]  view plain copy

  1. function Controller($scope) {  
  2.     $scope.name = 'Pajjket';  
  3.     $scope.age = 99;  
  4.     $scope.sex = '我是男的';  
  5.     $scope.say = function() {  
  6.         alert('Hello,我是弹出消息');  
  7.     };  
  8. }  

那这几种修饰符的含义又是什么呢,他们如何关联起来的

”=“:指令中的属性取值为controller中对应$scope上属性的取值,可用于双向数据的绑定

”@“:指令中的取值为html中的字面量/直接量;建立一个local scope property到DOM属性的绑定。因为属性值总是String类型,所以这个值总是返回一个字符串。如果没有通过@attr指定属性名称,那么本地名称将与DOM属性的名称一直。例如<widget my-attr=”hello {{name}}”>,widget的scope定义为:{localName:’@myAttr’}。那么,widget scope property的localName会映射出”hello {{name}}"转换后的真实值。name属性值改变后,widget scope的localName属性也会相应地改变(仅仅单向,与下面的”=”不同)。name属性是在父scope读取的(不是组件scope)

”&“:指令中的取值为Contoller中对应$scope上的属性,但是这属性必须为一个函数回调

继续阅读