0
我用決心試圖延遲加載控制器內部路線:defer.promise沒有等待的對象,以解決
.when('/somepage', {
resolve: {
load: function (loadDependencies, $q) {
return loadDependencies.load(['controllers/myCtrl.js'], [], []);
}
},
templateUrl: 'views/some-template.html'
})
這裏是我的loadDependencies廠:
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;
}
}
});
我是新角度,但從我的理解 - deferred.promise應等待承諾解決?目前它只是返回對象。我也試過這個:
deffered.promise.then(function() {
// call back here
});
但我沒有理解如何將解析的值返回給控制器。
承諾不是'return'值。 – Bergi 2014-10-27 12:06:10