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

输入 hellobug 

失去焦点后提示 hellobug 这个用户名已经存在 

代码如下:

1)

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

controller和directive的定义

app.controller('MainCtrl', function($scope) {
$scope.checkUsername = function() {
//send ajax to check on server
if ($scope.username === 'hellobug') {
$scope.usernameAlreadyExist = true;
}
}
}); app.directive('ngBlur', function($document) {
return {
link: function(scope, element, attrs) {
$(element).bind('blur', function(e){
scope.$apply(attrs.ngBlur);
});
}
}
})

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

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

directive的简单写法

app.directive('ngBlur', function($document) {
return function(scope, element, attrs) {
$(element).bind('blur', function(e){
scope.$apply(attrs.ngBlur);
});
};
})

2)也可用$parse服务

app.directive('focusDir',[ "$timeout","$parse" ,function($timeout,$parse) {
return {
link: function($scope, element, attrs) {
var model = $parse(attrs.focusDir);
$scope.$watch(model, function(value) {
// console.log('value=',value);
if(value === true) {
$timeout(function() {
element[0].focus();
});
}
});
// to address @blesh's comment, set attribute value to 'false'
// on blur event:
element.bind('blur', function() {
// console.log('blur');
$scope.$apply(model.assign($scope, false));
});
}
};
}] );

//Tip:记得 引入"$timeout","$parse"


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

<ul repeater="20">
<li>哈哈哈哈</li>
</ul>

directive的定义

app.directive('repeater', function($document) {
return {
restrict: 'A',
compile: function(element, attrs) {
var template = $(element).children().clone();
for(var i=0; i<attrs.repeater - 1; i++) {
$(element).append(template.clone());
}
}
}
});

在上面例子的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元素的一些行为

最后~

本节目所用示例可以猛戳这里查看ho~

Angularjs Directive - Compile vs. Link的更多相关文章

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

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

  2. angularjs指令中的compile与link函数详解(转)

    http://www.jb51.net/article/58229.htm 通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link, ...

  3. 【转】angularjs指令中的compile与link函数详解

    这篇文章主要介绍了angularjs指令中的compile与link函数详解,本文同时诉大家complie,pre-link,post-link的用法与区别等内容,需要的朋友可以参考下   通常大家在 ...

  4. angularjs指令中的compile与link函数详解

    这篇文章主要介绍了angularjs指令中的compile与link函数详解,本文同时诉大家complie,pre-link,post-link的用法与区别等内容,需要的朋友可以参考下   通常大家在 ...

  5. angularjs指令中的compile与link函数详解补充

    通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link,post-link的用法与区别. angularjs里的指令非常神奇,允许你 ...

  6. Directive Controller And Link Timing In AngularJS

    I've talked about the timing of directives in AngularJS a few times before. But, it's a rather compl ...

  7. angularJS directive详解

    前言 最近学习了下angularjs指令的相关知识,也参考了前人的一些文章,在此总结下. 欢迎批评指出错误的地方. Angularjs指令定义的API AngularJs的指令定义大致如下 angul ...

  8. angularjs directive 实例 详解

    前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的mo ...

  9. AngularJS Directive 学习笔记

    指令 Directive 指令要点 大漠老师的教学节点 解析最简单的指令 hello: 匹配模式 restrict 解析最简单的指令 hello: template.tempmlateUrl.$tem ...

随机推荐

  1. EntityFramework:迁移工具入门

    背景 刚毕业做项目的时候,没有用“迁移”这个概念,系统发布和更新的过程让人非常痛苦,在学习 Ruby On Rails 的过程解除了“迁移”,以后的所有项目都会先确定好“迁移”的方案,本文介绍一下En ...

  2. JDK JRE区别

    JDK里面的工具也是用JAVA编写的,它们本身运行的时候也需要一套JRE,如C:/Program Files/Java/jdk1.5.x/目录下的JRE.而C:/Program Files/Java/ ...

  3. golang日期时间格式format()

    format()函数格式化字符串,用了语句time.now().format(“2015-11-12 12:00:00”),结果输出结果就是不能达到理想的结果,然后把golang文档中的”2006-0 ...

  4. Servlet学习笔记(二):表单数据

    很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法. 1.GET 方法:GET 方法 ...

  5. 解决 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile)

    在项目构建的时候遇到了这样的问题:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile ...

  6. [AngularJS] Angular 1.3 new $q constructor

    <!DOCTYPE html> <html ng-app="app"> <head lang="en"> <meta ...

  7. C#基础视频教程2 常见数据类型和属性方法

    记住只要掌握常用的几种数据类型即可(比如Single就很少用了,要用浮点数一般就用Double,现在的计算机内存已经大到不需要你去考虑优化什么了) 比如仅仅是整形就有9种,实际上我们只需要知道int和 ...

  8. Graph Automata Player

    题目action=problem&type=show&id=12839&courseid=269">here 第一道高速幂.同一时候也是第一道高斯消元. 输入的 ...

  9. angularjs中ng-show的使用

    ng-show 指令在表达式为 true 时显示指定的 HTML 元素,否则隐藏指定的 HTML 元素.下面是示例: <!doctype html> <html lang=" ...

  10. C++ 11 - STL - 函数对象(Function Object) (中)

    我们再来看一个复杂的例子 需求: 我们需要对集合内每个元素加上一个特定的值 代码如下: AddInt.h class AddInt { private: int theValue; // the va ...