13

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?

MForce22
  • 141
  • 1
  • 5
  • Can you provide a fiddle with what you are attempting to do? – ganaraj Jun 07 '12 at 08:49
  • Here is the fiddle http://jsfiddle.net/MForce/WVz8r/32/ take a look as well at the version http://jsfiddle.net/MForce/WVz8r/31/ 31 version works because I'm loading the modules together with the app initialization. But that it's exactly what I do not want to do, since I do not know what kind of directives will my template contain, and I will be loading the directives modules in separate scripts asynchronously. So basically once the App module starts, is there any way to add/inject additional modules to make it aware of new directives? – MForce22 Jun 07 '12 at 21:22
  • Currently, there is no "nice" way of doing this. Although, it's definitely on our list to do. Search mailing list for some ideas how to hack it :-D – Vojta Jun 10 '12 at 06:56
  • Still no luck I created my own version of the bootstrap method from the angularjs source passing it in the rootScope to avoid it to create a new scope but it still creates it screwing all my bindings on the new directives. Do you know how do I inject a module directive into the App module? – MForce22 Jun 12 '12 at 18:59
  • Any luck ??? trying to do the same here. – nolazybits Feb 01 '13 at 03:37
  • Before I post my solution, I want to see if I'm on the right page. You can lazy load controllers, directives, and services into an existing module, but you can't load a module itself. I simulated lazy-loading modules by making a system that consumes all of a module's pieces into the main module. You just have the be cautious of naming (don't let main module's name's clash with lazy-loaded module's names). Does this sound like the solution you need? – m59 Aug 20 '13 at 18:56
  • For anyone looking forward into the answer, take a look at here: http://stackoverflow.com/questions/18591966/inject-module-dynamically-only-if-required – gustavohenke Sep 27 '13 at 00:41

2 Answers2

3

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.

Community
  • 1
  • 1
Jussi Kosunen
  • 8,277
  • 3
  • 26
  • 34
3

Perhaps this can help?

app.requires.push("myModule");

injecting module when you have access only to a module variable

Community
  • 1
  • 1
Rasmus-E
  • 832
  • 1
  • 8
  • 15