我写了一个“light-gallery”指令。它需要 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
}
}
})
这个方法正确且正规吗?或者说我应该如何定期写作?
谢谢!
最佳答案
您要查找的是$timeout(fn)
。
fn
将在 Controller 渲染后调用。
关于javascript - Angular 指令不会发出 'viewContentLoaded' 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513225/