简体   繁体   English

某些事件后加载angularjs模块

[英]Load angularjs modules after some event

I am running into some special situation with angularjs and cordova . 我正在使用angularjscordova angularjs一些特殊情况。 Basically I want to load angular modules only after deviceready in cordova is fired. 基本上我只想在cordova中的deviceready后才加载角度模块。 My skeleton is somewhat like below in app.js : 我的骨架有点像下面的app.js

angular.module('app', [
'module1',
'module2',
'module3'
])
.run(function() {
});

I could check for any ready event in run() but it happens only after all the modules are executed. 我可以在run()检查是否有任何就绪事件,但是只有在所有modules执行完之后才会发生。 I don't want to "micro" check for ready event in each controller. 我不想“微”检查每个控制器中的就绪事件。

Two questions: 两个问题:

  1. Is there a way to load angular modules only after some event ready ( deviceready specifically)? 有一种方法只有一些事件就绪(后加载角模块deviceready特异性)?

  2. Bonus : Is there a way to load module1 first, detect for some event ready, then load module2 and module3 ? 好处 :有什么方法可以先加载module1 ,检测是否有事件准备好,然后再加载module2module3

Yes for both questions; 两个问题都可以。 it is possible to load modules on demand in AngularJs. 可以按需在AngularJs中加载模块。

Here is a link that contains an in-depth explanation on how to load modules as well as an example project containing example source code: http://blog.getelementsbyidea.com/load-a-module-on-demand-with-angularjs/ 这是一个包含有关如何加载模块的深入说明的链接,以及包含示例源代码的示例项目: http : //blog.getelementsbyidea.com/load-a-module-on-demand-with-angularjs /

The source code of AngularJs actually contains a list of all the modules. AngularJs的源代码实际上包含所有模块的列表。 However, this list is private, so cannot be modified. 但是,此列表是私有的,因此无法修改。 To get around this, the author carefully imitated the loading of modules in the AngularJs code in order to load modules at a later point. 为了解决这个问题,作者仔细地模仿了AngularJs代码中的模块加载,以便在以后加载模块。

I would recommend reading the article carefully so you understand everything and then adapting his code to your own purpose. 我建议您仔细阅读本文,以便您了解所有内容,然后根据自己的目的调整他的代码。

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

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