首页 > 其他 > 详细

angular $compiler

时间:2016-05-17 19:33:58      阅读:254      评论:0      收藏:0      [点我收藏+]

directive是如何被compiled

HTML编译发生在三个阶段:

1.$compile遍历DOM节点匹配directives

如果compiler找到元素上的directive,directive就会被加入匹配DOM元素的directives list列表中,一个元素可以有多个directives

2.绑定在DOM元素上的所有directives一旦被确定,compiler会按优先级给directives排序

每个directive的compile函数都会被执行。每个compile函数都有一次改变DOM的机会。每个compile函数都返回link函数。这些函数调用每个directive返回的link函数构成组合链接函数

3.$compile通过调用上一步讲述的组合链接函数来链接scope和template。依次调用directives的link函数,给每个directive配置注册元素监听事件,设置scope的$watch监听器

 

var $compile = ...; // injected into your code
var scope = ...;
var parent = ...; // DOM element where the compiled template can be appended

var html = ‘<div ng-bind="exp"></div>‘;

// Step 1: parse HTML into DOM element
var template = angular.element(html);

// Step 2: compile the template
var linkFn = $compile(template);

// Step 3: link the compiled template with the scope.
var element = linkFn(scope);

// Step 4: Append to DOM (optional)
parent.appendChild(element);

 

angular $compiler

原文:http://www.cnblogs.com/echo2016/p/5502695.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!