嗨,我想知道, 如果有一种方法可以获得类似承诺的东西(我想在这种情况下它将是postLink)在$ scope上。 我在这里是一个悬而未决的问题:AngularJS: how to know when the $compile has finished?这与我的问题非常相似,
但我想知道是否有人可以帮助我理解如何挂钩postLink事件:
我有以下代码:
var compiledEl =
$compile("<link data-ng-repeat='stylesheet in injectedStylesheets' data-ng-href='{{stylesheet.href}}' rel='stylesheet' />");
head.append(compiledEl(scope));
所以我不知道在哪里添加promise.resolved(true);
因为我没有看到postLink。我怎么知道$ compile结束的时候。
感谢
答案 0 :(得分:3)
你可以自己做一个指令:
app.directive('compileCallback',function(){
return {
priority: 1001, // make sure it's the last to run
link: function (scope, element, attrs){
scope.$eval(attrs.compileCallback);
}
}
});
在模板中:
<link data-ng-repeat='stylesheet in injectedStylesheets' data-ng-href='{{stylesheet.href}}' rel='stylesheet' compile-callback="promiseResolve()" />
答案 1 :(得分:0)
基于角度文档,它说:
Directives with greater numerical priority are compiled first.
所以,我不认为在这里设置这个优先级值是正确的,意图是它将在最后运行。如果我错了,请纠正我。
虽然这个指令会在函数完成时调用它。
另外,我会检查以确保compileCallback
设置为:
if (angular.isFunction(scope.compileCallback)) {
scope.compileCallback();
}