tem*_*ame 0 angularjs angularjs-directive angularjs-ng-transclude
我正在编写一个使用transclusion的自定义指令.我需要在进行转换后对已转换的DOM内容进行修改....我想在后链接或控制器中执行此操作还是...链接...?我不知道transclusion在哪里发生的事情顺序,所以我不知道如何确保我的JS在转换发生后执行.
转换发生在编译阶段.Angular的$compile
服务遍及DOM,如果指令启用了transclusion,Angular会提取元素的内容(if transclude: true
,或元素本身 - for transclude: 'element'
),编译它们,并使编译内容在transclusion函数中可用,绑定到划分范围.
所以,执行顺序是:
compile
函数执行compile
指令的函数执行 - 返回pre
- 和 - post
链接函数controller
指令的功能执行pre
指令的 -link函数执行post
指令的 -link函数执行.您可以决定transclude
在post
-link函数中执行该函数.一旦你这样做,
controller
执行包含transcluded内容指令的,pre
- 和post
-link函数transclude
函数在步骤#3-#5中可用,并且在调用时,它提供了被转换元素的新副本cloneAttachFn
,您将其作为transclude
函数的参数提供:
link: function(scope, element, attrs, ctrls, transclude){
transclude(scope, function cloneAttachFn(clone){
// you can do DOM manipulation of the clone instance here
});
}
Run Code Online (Sandbox Code Playgroud)
请注意,该DOM中的DOM操作cloneAttachFn
将应用于已转换DOM的实例 - 而不是预编译的DOM模板.
归档时间: |
|
查看次数: |
465 次 |
最近记录: |