Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - angularjs inject module after app initialization

Hello everyone I've been trying to do this with no luck for a few days.

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.

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. :(

Any help around here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's ugly, but it works: http://jsfiddle.net/MzseV/7/

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.

Here's the question I asked and later answered about this.

EDIT: just noticed how old this question is, hope it still helps someone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...