AngularJS bootstrap or load controllers on the fly -


i think answer question "no" i'm asking anyways cant seem find definitive answer.

can angularjs load controllers on fly?

to explain further, have angular app bootstrapped default way, via ng-app.example, index.html file:

<!doctype html> <html> <head>     <title>myapp</title> </head> <body ng-app="app"> ... <app-body></app-body> <script src="lib/angular/angular.min.js"></script> <script src="app/app.js"></script> ... 

inside app module, have route setup, this:

$routeprovider           .when('/home', {               templateurl: 'home/home.html',               controller: 'homecontroller'           })           .when('/behaviors', {               templateurl: 'contact/contact.html',               controller: 'contactcontroller'           });         // etc. 

i curious - there way load homecontroller.js file when user goes route? js file not set in index.html above, inside home/home.html:

<script src="home/homecontroller.js"></script> 

is possible?

yes, angularjs able dynamically load route controllers , dependencies such factories. require use of requirejs. requirejs entry point, load angular.

the angular route's resolve property kick off call code resolved file via requirejs. since resolve property utilizes promises route won't load until file loaded.

alternatively optimize app using gulpjs, gulp-angular-template cache, concat, uglify, etc. here example:

http://weblogs.asp.net/dwahlin/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs


Comments