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 complicated topic, so I don't mind digging a bit deeper. This time, I'm looking at the timing of directive controllers vs. directive link functions. As the DOM (Document Object Model) is compiled by AngularJS, the directive controllers and link functions execute at different parts of the compile lifecycle.
When AngularJS compiles the DOM, it walks the DOM tree in a depth-first, top-down manner. As it walks down the DOM, it instantiates the directive controllers. Then, when it gets to the bottom of a local DOM tree branch, it starts linking the directives in a bottom-up manner as it walks back up the branch. This doesn't mean that all directive controllers are run before all directive linking; it simply means that in a local DOM branch, the directive controllers are instantiated before they are linked.
To see this in action, I've put together a very simple DOM tree in which each element has a unique (but almost identical) directive. As each directive controller and link function is executed, it will log to the console. This way, we can see the timing of the various methods in relation to the DOM tree structure.
<!doctype html>
<html ng-app="Demo">
<head>
<meta charset="utf-8" /> <title>
Directive Controller And Link Timing In AngularJS
</title>
</head>
<body> <h1>
Directive Controller And Link Timing In AngularJS
</h1> <div bn-outer> <p bn-mid> <span bn-inner> Woot! </span> </p> <p bn-second-mid> Woot, indeed! </p> </div> <!-- Load scripts. -->
<script type="text/javascript" src="../../vendor/jquery/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="../../vendor/angularjs/angular-1.2.4.min.js"></script>
<script type="text/javascript">
// Create an application module for our demo.
var app = angular.module( "Demo", [] );
// -------------------------------------------------- //
// -------------------------------------------------- //
// I demonstrate the timing of directive execution.
app.directive(
"bnOuter",
function() {
function Controller( $scope ) {
console.log( "Outer - Controller" );
}
function link( $scope, element, attributes, controller ) {
console.log( "Outer - Link" );
}
// Return directive configuration.
return({
controller: Controller,
link: link
});
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// I demonstrate the timing of directive execution.
app.directive(
"bnMid",
function() {
function Controller( $scope ) {
console.log( "Mid - Controller" );
}
function link( $scope, element, attributes, controller ) {
console.log( "Mid - Link" );
}
// Return directive configuration.
return({
controller: Controller,
link: link
});
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// I demonstrate the timing of directive execution.
app.directive(
"bnSecondMid",
function() {
function Controller( $scope ) {
console.log( "Second Mid - Controller" );
}
function link( $scope, element, attributes, controller ) {
console.log( "Second Mid - Link" );
}
// Return directive configuration.
return({
controller: Controller,
link: link
});
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// I demonstrate the timing of directive execution.
app.directive(
"bnInner",
function() {
function Controller( $scope ) {
console.log( "Inner - Controller" );
}
function link( $scope, element, attributes, controller ) {
console.log( "Inner - Link" );
}
// Return directive configuration.
return({
controller: Controller,
link: link
});
}
);
</script> </body>
</html>
Before we look at the console output, take note that there are two "mid" branches. This means that AngularJS has two branches to explore before it can fully walk back up to the top DOM node. That said, when we do run the above code, we get the following console log output:
Outer - Controller
Mid - Controller
Inner - Controller
Inner - Link
Mid - Link
Second Mid - Controller
Second Mid - Link
Outer - Link
As you can see, as AngularJS walks the DOM tree, it instantiates the directive controllers on the way down; then, it links the directives on the way back up.
This is an important difference. While you can only access the DOM tree in the bottom-up linking phase, the directive controller can provide a hook into the top-down lifecycle. This can be critical if you have to handle DOM events based on when elements of the DOM tree came into existence. The linking phase can never give you that because it's executed in reverse.
Directive Controller And Link Timing In AngularJS的更多相关文章
- angularJS directive中的controller和link function辨析
在angularJS中,你有一系列的view,负责将数据渲染给用户:你有一些controller,负责管理$scope(view model)并且暴露相关behavior(通过$scope定义)给到v ...
- 【转载】AngularJs 指令directive之controller,link,compile
关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-controller.不过你可千万不要用 ng-前缀了,防止与系统自带的指令重名.另外一个需知道的 ...
- 49.AngularJs 指令directive之controller,link,compile
转自:https://www.cnblogs.com/best/tag/Angular/ 关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-cont ...
- AngularJS之指令中controller与link(十二)
前言 在指令中存在controller和link属性,对这二者心生有点疑问,于是找了资料学习下. 话题 首先我们来看看代码再来分析分析. 第一次尝试 页面: <custom-directive& ...
- AngularJS的指令(Directive) compile和link的区别及使用示例
如果我想实现这样一个功能,当一个input失去光标焦点时(blur),执行一些语句,比如当输入用户名后,向后台发ajax请求查询用户名是否已经存在,好有及时的页面相应. 输入 camnpr 失去焦点后 ...
- Angularjs Directive - Compile vs. Link
如果我想实现这样一个功能,当一个input失去光标焦点时(blur),执行一些语句,比如当输入用户名后,向后台发ajax请求查询用户名是否已经存在,好有及时的页面相应. 输入 hellobug 失去 ...
- anagularJs指令的controller,link,compile有什么不同
/directives.js增加exampleDirective phonecatDirectives.directive('exampleDirective', function() { retur ...
- controller,link,compile不同
测试案例 .directive('testDirective', function() { return { restrict: 'E', template: '<p>Hello {{nu ...
- angularjs1.x的directive中的link参数element见解
angular.module("APP",[]) .directive("testDw",function () { return{ restrict:&quo ...
随机推荐
- Linux内核中的GPIO系统之(3):pin controller driver代码分析--devm_kzalloc使用【转】
转自:http://www.wowotech.net/linux_kenrel/pin-controller-driver.html 一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道 ...
- Oracle基础 07 参数文件 pfile/spfile
--查看数据库运行模式(spfile还是pfile)select decode(count(*),1,'spfile','pfile') from v$spparameterwhere rownum= ...
- [ 总结 ] 删除通过find查找到的文件
[root@cloud abc]# touch test{,,,} [root@cloud abc]# ls shadow test test1 test2 test3 test5 [root@clo ...
- k8s的网络学习
1.Kubernetes 网络模型 Kubernetes 采用的是基于扁平地址空间的网络模型,集群中的每个 Pod 都有自己的 IP 地址,Pod 之间不需要配置 NAT 就能直接通信.另外,同一个 ...
- opencv mat
mat基础教程: http://blog.csdn.net/sinat_31802439/article/details/50083291 mat 初始化: Mat M(,,CV_32FC1); Ma ...
- 关于MYSQL表记录字段换行符回车符处理
http://hualong.iteye.com/blog/1933023 今天遇到一个非常奇葩的问题,数据库表中明明有值却查询不不出来,而然一次从单元格中复制到sql中,发现右侧单引号换行了,我初步 ...
- [DB2]Linux下安装db2 v9.7
https://www.cnblogs.com/cancer-sun/p/5168728.html
- django CXRF介绍
CSRF(Cross-site request forgery)跨站请求伪造,是攻击者利用用户的身份操作用户帐户的一种攻击方式.和XSS攻击一样,存在巨大的危害性. 一.攻击方法 1.低级的CXRF攻 ...
- 单词接龙(dragon)(BFS)
单词接龙(dragon) 时间限制: 1 Sec 内存限制: 64 MB提交: 12 解决: 5[提交][状态][讨论版] 题目描述 单 词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已 ...
- centos 7下查找大文件、大目录和常见文件查找操作
根据园子 潇湘隐者的文章 <Linux如何查找大文件或目录总结>结合实际运维需要整理出常用命令 目标文件和目录查找主要使用 find 命令 结合 xargs (给命令传递参数的一个过滤器, ...