how to lazyload angular's controllers #2053
Comments
no ask for me? )-: |
@mz121star in the current version AngularJS doesn't provide special mechanism to load controllers (not other services, filters etc. for that matter) on demand. Currently a controller needs to present (loaded) when it gets instantiated. There were several threads on the mailing list and Stack Overflow regarding this and several people had some success with loading globally defined controllers on demand. Search archives of the mailing list / SO to learn more. You might also watch best practices video where this topic is mentioned: http://www.youtube.com/watch?v=ZhfUv0spHCY&feature=g-user-u Future version of AngularJS might have support for lazy-loading controllers. Going to close this issue for now, please don't hesitate to post on the mailing list (angular@googlegroups.com) if you need more info. |
Thank you for your reply! |
FYI, I have crated a lazy load directive that allows you to lazy load the controllers when globally defined.
All the angular team would have to do is support adding controllers to modules on the fly (without requiring re-bootstrapping) and the default controller definitions would work. |
+1 |
I have written a blog post showing how you can implement lazy loading in AngularJS. http://ify.io/entry/172/lazy-loading-in-angularjs/ |
Do somebody knows when AngularJS will have a native feature to do it ? |
Tested solution provided by @ifyio: works fine! Great work! |
The directive presented by @timothyswt didn't work for me and the solution from @ifyio is dealing with routes whereas I am more interested in dynamically loading a controller with ng-include. I copied that directive and modified it to load both a js and html file, register the controller, and add the controller to the partial's div. My solution is probably very bad (I'm a noob), but it is working as I intended. I would love to see how it can be improved. Detailed explanation here: http://pastebin.com/RsYuKTMT Edit: I changed the link above. My previous link was not functioning as I thought. I have greatly improved my approach in many regards, as I am understanding the original ng-include directive better. |
The @ifyio solution works well ! Thanks to him. |
@mz121star +1 |
You might also want to take a look at angularAMD: or ng-couch-potato: |
I want uses angularjs and requirejs ,
but now, when i wrote routeProvider need require() all controlls module.
how to lazyload my controllers module?
The text was updated successfully, but these errors were encountered: