As I work on developing the architecture of a complex application with Angular, I have started with the angular-seed project which seems to be a solid starting point. However, one issue that concerns me is how Angular apps tend to load everything upfront by nature, making it difficult to implement lazy loading using script loaders.
Coming from a background in backbone.js, where I used require.js for lazy loading based on router callbacks, handling routes in Angular feels somewhat different. The code snippet below demonstrates how routes are typically defined:
// Declare app level module which depends on views, and components
angular.module('myApp', [
'ngRoute'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when({templateURL:'../tmpl.html',controller:'view1Ctrl'})
.when({templateURL:'../tmpl.html',controller:'view1Ctrl'})
.otherwise({redirectTo: '/view1'});
}]);
In this line
$routeProvider.when({templateURL:'../tmpl.html',controller:'view1Ctrl'})
, my goal is to find a way to lazily load both the controller and the template. I considered using something like:
$routeProvider.when({templateURL:'../tmpl.html',controller:require('view1Ctrl'})
with browserify, but it doesn't feel like a clean solution even with require included. I know similar questions have been raised on SO before, but I am yet to find a definitive answer.
Personally, I prefer using browserify as it allows for using CommonJS modules in the browser, which is highly appreciated.