How can I inject an AngularJS module dependency without declaring it in the dependency array when the module is defined?

允我心安 提交于 2019-12-11 00:01:54

问题


Currently the only way that I know of to make module dependencies available is to specify them when a module is declared. e.g.:

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

This becomes a problem when the app gets large and modules have a lot of dependencies, libraries that must be loaded. If these libraries aren't required for any of the modules that the first page the user visits, it seems that those dependencies could be deferred to improve load time.

Any advice would be appreciated.


回答1:


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

Mention and example is here:

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

AngularJS - Injecting factory from another module into a provider




回答2:


you can use lazy load moduls. https://oclazyload.readme.io/docs



来源:https://stackoverflow.com/questions/37733304/how-can-i-inject-an-angularjs-module-dependency-without-declaring-it-in-the-depe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!