Angularjs Ng重复优先级
我有以下资料:Angularjs Ng重复优先级,angularjs,angularjs-directive,angularjs-ng-repeat,Angularjs,Angularjs Directive,Angularjs Ng Repeat,我有以下资料: app.directive("myDirective", function($compile) { return { replace: true, scope: {}, template: "<div></div>", link: function(scope, elem) { scope.list = [1, 2, 3, 4, 5];
app.directive("myDirective", function($compile) {
return {
replace: true,
scope: {},
template: "<div></div>",
link: function(scope, elem) {
scope.list = [1, 2, 3, 4, 5];
$compile("<div my-list></div>")(scope, function(clone) {
// Why the ng-repeat isn't compiled yet?
alert(clone[0].outerHTML);
elem.html(clone);
});
}
};
});
app.directive("myList", function() {
return {
replace: true,
template: "<ul><li ng-repeat=\"item in list\">{{item}}</li></ul>"
};
});
app.directive(“myDirective”),函数($compile){
返回{
替换:正确,
作用域:{},
模板:“”,
链接:功能(范围、要素){
scope.list=[1,2,3,4,5];
$compile(“”)(范围、函数(克隆){
//为什么ng repeat尚未编译?
警报(克隆[0]。outerHTML);
elem.html(克隆);
});
}
};
});
应用程序指令(“myList”,函数(){
返回{
替换:正确,
模板:“- {item}
”
};
});
有人能告诉我为什么我的没有处于警报状态吗
它使用$timeout指令()工作,冷却时间为“0”。听起来有点棘手,但是
因此,在控制器中注入$timeout,然后在回调中:
$timeout(function () {
alert(clone[0].outerHTML);
}, 0);
我猜编译
的任务是与子方向中li
s的生成同时计算的
这可以解释为什么您的警报发生在ng repeater
完全生成li
之前
因此,$timeout
对于线性化这两个任务非常有用
解释原因如下:好的,我将对@dawuut的答案再补充一点
在带有“0”冷却的$timeout
中包装警报将起作用,因为直到角度$digest
循环结束时才会触发超时
因此,当您第一次在父指令中调用alert
时,子指令尚未编译(它们都有自己的作用域)。我认为OP对“为什么”这是一种行为感兴趣。那么,为什么添加一个$timeout
会带来不同呢。