AngularJS的指令(Directive) compile和link的区别及使用示例

如果我想实现这样一个功能,当一个input失去光标焦点时(blur),执行一些语句,比如当输入用户名后,向后台发ajax请求查询用户名是否已经存在,好有及时的页面相应。

输入 camnpr
angularjs directive input focus

失去焦点后提示 camnpr 这个用户名已经存在 
angularjs directive input focus用户名已经存在

HTML代码如下:

1 <body ng-controller="MainCtrl">
2     <lable>用户名:
3         <input type="text" ng-model="username" ng-blur="checkUsername()" />
4         <span style="color:red;" ng-show="usernameAlreadyExist">用户名已经存在</span>
5     </lable>
6 </body>

controller和directive的定义

01 app.controller('MainCtrl'function($scope) {
02     $scope.checkUsername = function() {
03         //send ajax to check on server
04         if ($scope.username === 'hellobug') {
05            $scope.usernameAlreadyExist = true;
06         }
07     }
08 });
09 app.directive('ngBlur'function($document) {
10     return {
11         link: function(scope, element, attrs) {
12             $(element).bind('blur'function(e){
13                 scope.$apply(attrs.ngBlur);
14             });
15         }
16     }
17 })

在上面的例子里,directive返回对象里定义的link方法在blur事件触发时执行了scope上的checkUsername()方法。

如果是只有link方法,也可以简单的写成下面这种形式~直接返回link对应的function~

directive的简单写法

1 app.directive('ngBlur'function($document) {
2     return function(scope, element, attrs) {
3         $(element).bind('blur'function(e){
4             scope.$apply(attrs.ngBlur);
5         });
6     };
7 })

再来这样一个功能,我想让内容为哈哈哈哈的dom元素重复n遍,n是自定义的,以达到某种满屏大笑丧心病狂的效果 -_-,我知道ng-repeat就已经能干这事儿了,但是如果自己实现一下呢~

HTML

1 <ul repeater="20">
2     <li>哈哈哈哈</li>
3 </ul>

directive的定义

01 app.directive('repeater', function($document) {
02     return {
03         restrict: 'A',
04         compile: function(element, attrs) {
05             var template = $(element).children().clone();
06             for(var i=0; i<attrs.repeater - 1; i++) {
07                 $(element).append(template.clone());
08             }
09         }
10    }
11 });

在上面例子的compile方法里,子元素被复制成了repeater制定的数量。


什么时候用compile,什么时候用link呢,或者两者可不可以一起用呢?

先从directive是如何在angular手下生效的说起吧~

编译三阶段:

1. 标准浏览器API转化

将html转化成dom,所以自定义的html标签必须符合html的格式

2. Angular compile

搜索匹配directive,按照priority排序,并执行directive上的compile方法

3. Angular link

执行directive上的link方法,进行scope绑定及事件绑定

为什么编译的过程要分成compile和link?

简单的说就是为了解决性能问题,特别是那种model变化会影响dom结构变化的,而变化的结构还会有新的scope绑定及事件绑定,比如ng-repeat

compilelink的形式

compile

  • function compile(element, attrs, transclude) { ... }
  • 在compile阶段要执行的函数,返回的function就是link时要执行的function
  • 常用参数为elementattrs,分别是dom元素和元素上的属性们,其它的以后细说
  • 较少使用,因为大部分directive是处理dom元素的行为绑定,而不是改变它们

link

  • function link(scope, element, attrs, controller) { ... }
  • 在link阶段要执行的函数,这个属性只有当compile属性没有设置时才生效
  • 常用参数为scopeelementattrs,分别是当前元素所在的scope,dom元素和元素上的属性们,其它的以后细说
  • directive基本上都会有此函数,可以注册事件,并与scope相绑

compilelink的使用时机

compile

  • 想在dom渲染前对它进行变形,并且不需要scope参数
  • 想在所有相同directive里共享某些方法,这时应该定义在compile里,性能会比较好
  • 返回值就是link的function,这时就是共同使用的时候

link

  • 对特定的元素注册事件
  • 需要用到scope参数来实现dom元素的一些行为
原创文章如转载,请注明:转载自郑州网建-前端开发  http://camnpr.com/
本文链接: http://camnpr.com/javascript/1716.html
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值