【问题标题】:How can I inject an AngularJS module dependency without declaring it in the dependency array when the module is defined?如何在定义模块时注入 AngularJS 模块依赖项而不在依赖项数组中声明它?
【发布时间】:2016-06-09 18:10:51
【问题描述】:

目前,我所知道的使模块依赖项可用的唯一方法是在声明模块时指定它们。例如:

angular.module("myApp",["myDependentModule"]);

当应用程序变大并且模块有很多依赖项、必须加载的库时,这会成为一个问题。如果用户访问的第一页的任何模块都不需要这些库,那么似乎可以推迟这些依赖项以缩短加载时间。

任何建议将不胜感激。

【问题讨论】:

    标签: angularjs webpack oclazyload


    【解决方案1】:

    使用 angular.injector(...) https://docs.angularjs.org/api/ng/function/angular.injector

    提及和示例在这里:

     var injector = angular.injector(['someModule']);
     var someService = injector.get('someService');
    

    AngularJS - Injecting factory from another module into a provider

    【讨论】:

      【解决方案2】:

      您可以使用延迟加载模块。 https://oclazyload.readme.io/docs

      【讨论】:

      • 我用过这个库,但没有成功让它加载模块。我真正想做的是使依赖模块在 UI 路由器中可用。也许我需要调整我的模式,因为我似乎遇到了一个限制,即在加载这些模块之前它无法知道其他模块中指定的路由,因此它不能延迟加载它们。
      猜你喜欢
      • 2015-10-06
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2019-12-13
      • 2015-06-06
      相关资源
      最近更新 更多