简体   繁体   English

Angular指令不会发出'viewContentLoaded'事件

[英]Angular directives do not emit 'viewContentLoaded' event

I wrote a 'light-gallery' directive. 我写了一个“ light-gallery”指令。 It needs jquery.lightgallery.js plugin to initialize $.fn.lightGallery() functions which must do after the directive template compiled and loaded. 它需要jquery.lightgallery.js插件来初始化$.fn.lightGallery()函数,这些函数必须在编译和加载指令模板之后执行。

However, Angular directives do not emit 'viewContentLoaded' event in the link function. 但是,Angular指令不会在link函数中发出“ viewContentLoaded”事件。 So I want to ask how I can know when the directives templates compiled and loaded? 所以我想问我如何知道指令模板何时被编译和加载?

Now I used $timeout to listen for a scope value if it has been compiled. 现在,我使用$timeout监听范围值(如果已编译)。 When the scope value has been compiled, the oncompiled function can be run. 范围值已编译后,可以运行oncompiled函数。 Look: 看:

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
    }
  }
})

Is that method right and regular? 那是正确和常规的方法吗? Or how should I write regularly? 还是应该定期写?

Thanks! 谢谢!

What you looking for is $timeout(fn) . 您要寻找的是$timeout(fn)
The fn will be called after your controller rendered. 在渲染控制器之后,将调用fn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM