angularjs应用程序初始化后注入模块 [英] angularjs inject module after app initialization
问题描述
大家好我一直在努力,没有运气了几天来做到这一点。
Hello everyone I've been trying to do this with no luck for a few days.
反正是有异步加载一些JS脚本(包含角模块),它已被初始化后,其注入到正在运行的应用程序。
is there anyway to load asynchronously some js scripts(which contain angular modules) and inject them to the running app after it has been initialized.
基本上我有基于此HTML code,我会决定哪一个js文件(模块),将与从服务器请求一些HTML被填充的容器DOM元素,将被要求,然后加载它们异步地注入模块对角应用程序,并编译新注入的模块的内容。
Basically I have a container DOM element that will be populated with some HTML requested from the server, based on this HTML code I will decide which js files (modules) will be required, then load them asynchronously inject the modules to the angular app and compile the content with the new injected modules.
我试过,但我每次做引导方法注入模块时的主控制器激发起来并删除我所有的范围状态。 (
I tried but every time I do the bootstrap method injecting the module the Main Controller fires up and erases all my scope state. :(
任何帮助在这里?
推荐答案
这是丑陋的,但它的工作原理: HTTP: //jsfiddle.net/MzseV/7/
It's ugly, but it works: http://jsfiddle.net/MzseV/7/
它基本上是通过遍历模块的 _invokeQueue
成员,并在使用用于注册服务,控制器和指令(注提供商应用功能的工作原理:这些都需要引导前被捕获据我所知)。这大概也重新注册你previously注册,所以你可能需要一些启发式只挑选您想要的任何东西,但我不知道是否有在重新在注册任何损伤。
It basically works by iterating the module's _invokeQueue
member and applying functions within using the providers used for registering services, controllers and directives (NOTE: these need to be captured before bootstrapping AFAIK). This will probably also re-register anything you've previously registered so you might want some heuristic to only pick the ones you want, although I'm not sure if there's any damage in re-registring.
再次,这是相当可怕的和哈克所以我只能作为最后的手段使用。
Again, it's fairly hideous and hacky so I'd only use as a last resort.
<一个href=\"http://stackoverflow.com/questions/15250644/angularjs-loading-a-controller-dynamically\">Here's我问,后来的问题回答了这个问题。
Here's the question I asked and later answered about this.
编辑:刚刚注意到多大这个问题,希望它仍然可以帮助别人
just noticed how old this question is, hope it still helps someone.
这篇关于angularjs应用程序初始化后注入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!