I'm trying to lazy-load controllers inside route by using resolve:
.when('/somepage', {
resolve: {
load: function (loadDependencies, $q) {
return loadDependencies.load(['controllers/myCtrl.js'], [], []);
}
},
templateUrl: 'views/some-template.html'
})
Here's my loadDependencies factory:
app.factory('loadDependencies', function ($q, $timeout) {
return {
load: function (Controllers,cssFiles,modules) {
var jsPath = "scripts/",
cssPath = "css/",
head = document.getElementsByTagName("head")[0],
deffered = $q.defer(),
jsReady = 0,
jsShouldBeReady = Controllers.length;
Controllers.forEach(function (arrayItem) {
var js = document.createElement("script");
js.src = jsPath + arrayItem;
head.appendChild(js);
js.onload = function () {
jsReady++;
if (jsReady == jsShouldBeReady) { // if loaded files equal to controllers length, then they all finished loading - so resolve deffered
deffered.resolve(true);
}
};
js.onerror = function() {
alert("Cannot load js files. Pleae try again later");
};
});
return deffered.promise;
}
}
});
I'm new to angular, but from my understanding - deffered.promise should wait for the promise to resolve? currently it just returns the object. I also tried this:
deffered.promise.then(function () {
// call back here
});
But i'm failing to understand how to return the resolved value back to the controller.