angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Routes should allow to load modules/controllers on demand

Open zilluss opened this issue 13 years ago • 23 comments

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.

zilluss avatar Mar 10 '13 21:03 zilluss

+1

bkuhl avatar Apr 17 '13 18:04 bkuhl

+1

moisesbaddini avatar Jul 21 '13 16:07 moisesbaddini

+1

santyclaz avatar Aug 16 '13 15:08 santyclaz

+1

zaver avatar Aug 20 '13 18:08 zaver

+1

missinglink avatar Aug 28 '13 12:08 missinglink

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

spalger avatar Sep 04 '13 17:09 spalger

+1

felixdulfer avatar Sep 06 '13 22:09 felixdulfer

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:

cleyshan avatar Oct 09 '13 04:10 cleyshan

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

dgieselaar avatar Oct 30 '13 14:10 dgieselaar

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

Cosmitar avatar Feb 25 '14 04:02 Cosmitar

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

mokkabonna avatar Feb 25 '14 13:02 mokkabonna

+1

renato avatar Feb 28 '14 17:02 renato

+1

juji avatar Mar 25 '14 06:03 juji

+1

BaggersIO avatar Jun 18 '14 11:06 BaggersIO

:heart: google,this function is basic and obvious

hyper315 avatar Aug 18 '14 05:08 hyper315

+1

stefanomozart avatar Jul 30 '15 22:07 stefanomozart

+1

senthanal avatar Sep 27 '15 10:09 senthanal

+1

paul-ntech avatar Oct 15 '15 01:10 paul-ntech

+1

napstercake avatar Nov 30 '15 23:11 napstercake

+1

pyotruk avatar Oct 03 '16 11:10 pyotruk