如何检测所有指令何时加载?

时间:2014-11-05 17:20:29

标签: angularjs angularjs-directive

这是另一个问题(Angular: running function in controller when all directives are loaded)的衍生产品,因为这个问题很普遍。

如何检测页面上所有角度指令何时加载?

修改

到目前为止,我找到的唯一方法是在最高级别设置一个指令,在其链接功能期间触发范围功能。这可以用作角度编译页面上从外到内的所有指令,并从内到外调用链接函数。

下行:如果任何内部指令使用templateUrl,这将不再有效,因为这将发生异步。这将导致外部指令链接函数在内部指令链接函数之前运行。此外,您必须向所有视图/包含添加指令。

EDIT2:

我找到了一个很好的答案来解释上述问题。在评论中,j_walker_dev提到了以下内容:

The way i got around this was to use template, like mentioned. 
But to inject $templateCache and do template: $templateCache.get('url.tpl.html').

问题:How to execute parent directive before child directive?

至少有了这个,可以继续使用templateUrls,因为它避免了指令中模板加载的异步行为。

1 个答案:

答案 0 :(得分:0)

这是量角器同步的方式:

functions.waitForAngular = function(rootSelector, callback) {
  var el = document.querySelector(rootSelector);
  try {
    if (angular.getTestability) {
      angular.getTestability(el).whenStable(callback);
    } else {
      angular.element(el).injector().get('$browser').
          notifyWhenNoOutstandingRequests(callback);
    }
  } catch (e) {
    callback(e);
  }
};

知道缓存中的指令将同步执行,需要从服务器加载模板的指令将在$浏览器通知之前执行,这就是我要做的事情(如果等待ajax请求而不是模板不是问题)

相关问题