Angular.js: Routes should allow to load modules/controllers on demand

Created on 10 Mar 2013  Â·  23Comments  Â·  Source: angular/angular.js

When you route to a specific location, the template for that route is loaded on demand. There should also be a feature to specify a module or controller script that will be loaded when the user routes to the given location.

There is a solution using require.js (https://github.com/matys84pl/angularjs-requirejs-lazy-controllers), but it adds another library and another nesting level (angular module definitions within require.js module definitions)

As for the use case:

  • Increase the initial startup time by loading scripts only when they are needed.
  • Only serve scripts to users that have the right to see the route they are used in.
  • Enhanced modularity.
di ngRoute feature

Most helpful comment

In 1.6.7, it will be possible to load new modules into the injector after the app has been initialized, e.g. in a route's resolve function. You'll still have to provide a way to load and execute the script though. See https://github.com/angular/angular.js/commit/34237f929927295392bbb1a600e78cbde581839a for more info

All 23 comments

+1

+1

+1

+1

+1

+1, but even the ability to lazy load modules at all would be nice.

+1

I have made a very small change to angular that allows modules to load after bootstrap, which is more general than the lazy load controller approach at https://github.com/matys84pl/angularjs-requirejs-lazy-controllers.

Put these lines in createInjector(), just before the return statement (tested this on 1.2.0-rc.2):

instanceInjector.loadNewModules = function (mods) { forEach(loadModules(mods), function(fn) { instanceInjector.invoke(fn || noop); }); };

Then, after the module source is loaded dynamically (i use requireJS for now) just call $injector.loadNewModules(['nameOfModuleJustLoaded']), and all the components get registered nicely with angular (well the few things i've tested so far seem to work perfectly).

I'll try to make a PR out of this one day, when i get a spare moment...

:+1:

This (or just lazy loading at all indeed) is absolutely vital for single-page apps.

+1
I applied this workaround and solved my problem http://ify.io/lazy-loading-in-angularjs/ . Hope this help.

+1, with the ability to do another config for the loaded module and register new routes.

+1

+1

+1

:heart: google,this function is basic and obvious

+1

+1

+1

+1

+1

it's 2016. something should have been done..

:)

May the awesomeness will always be with you,

Tri Rahmat Gunadi - JUJI
http://jujiyangasli.com
Ph 0813.1012.8924

On Mon, Oct 3, 2016 at 7:02 PM, pyotruk [email protected] wrote:

+1

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/angular/angular.js/issues/2135#issuecomment-251088850,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAeH8RayUZA4MUmqWPnUIyb5S6Rsg5PRks5qwO64gaJpZM4AfSRI
.

The de-facto solution appears to be to use a 3rd party https://oclazyload.readme.io which appears to solve the problem in a generic way.

In 1.6.7, it will be possible to load new modules into the injector after the app has been initialized, e.g. in a route's resolve function. You'll still have to provide a way to load and execute the script though. See https://github.com/angular/angular.js/commit/34237f929927295392bbb1a600e78cbde581839a for more info

Was this page helpful?
0 / 5 - 0 ratings