保持服务的初始化,直到所有承诺都得到解决

Umu*_*acı 18 javascript angularjs

routeProvider我们可以容纳routing,如果我们给它包含的承诺下决心对象; 它会等到所有的承诺都解决了.但是,我无法在应用程序的初始化中找到方法.

除了angular.module("app", []).run(function (){ //init app })一个$resource$http哪个是异步之外,应用程序可以在解析依赖关系(promises)之前完成初始化,从而创建一个race condition.我们不希望这样.

所以问题是,是否有一种方法可以保持a的初始化,service直到所有给定的promise都被解析?

iwe*_*ein 5

我见过类似的问题.团队配合使用的一个优雅的解决方案是与RequireJS及其domReady模块的组合:

define(['require', 'angular', 'app','routes', 'pendingServices'], 
      function (require, ng, app, pendingServices) {


  /* place operations that need to initialize prior to app start here
   * using the `run` function on the top-level  module
   */
  app.run(pendingServices.init)

  require(['domReady!'], function (document) {
      /* everything is loaded...go! */
      ng.bootstrap(document, ['mainModule']);
  });

});
Run Code Online (Sandbox Code Playgroud)

在init方法中,您可以执行所有预加载(并等待所需的promise).我当然有兴趣听其他解决方案.