我写了一个'光廊'指令。它需要jquery.lightgallery.js
插件来初始化$.fn.lightGallery()
函数,这些函数必须在编译和加载指令模板后执行。
但是,Angular指令不会在link
函数中发出'viewContentLoaded'事件。所以我想问一下如何知道编译和加载指令模板的时间?
现在我使用$timeout
来监听范围值(如果已编译)。编译范围值后,可以运行oncompiled
函数。看:
myApp.directive("light-gallery", function(){
return {
restrict: "E",
scope: {},
replace: true,
template: '<div compiled="{{compiled}}"></div>',
link: function(scope, elem, attrs){
var onCompiled = function(){
$("#lightgallery").lightGallery();
};
var recur = function(){
// when the attr `compiled` values `true`
// it means template compiled and loaded
if("true" === elem.attr("compiled")){
onCompiled();
}else{
setTimeout(recur, 100);
}
};
recur();
// to tell template compiled
scope.compiled = true
}
}
})
这种方法是正确的吗?或者我该如何定期写作?
谢谢!
答案 0 :(得分:1)
您要找的是$timeout(fn)
控制器渲染后将调用fn
。