我有 2 个解决方案的问题,每个控制器一个解决方案。
http://jsfiddle.net/pvivera/RhAHy/
var app = angular.module('testApp', [], function($routeProvider){
$routeProvider.when('/', {
template: 'Home {{model}}',
controller: 'HomeCtrl',
resolve: HomeCtrl.resolve
})
.when('/About', {
template: 'About {{model}}',
controller: 'AboutCtrl',
resolve: AboutCtrl.resolve
});
});
var HomeCtrl = app.controller('HomeCtrl', function($scope, HomeCtrlData){
$scope.model = HomeCtrlData;
});
HomeCtrl.resolve = {
HomeCtrlData: function($q, $timeout){
var deferred = $q.defer();
$timeout(function(){
return deferred.resolve('AboutCtrlResolver');
}, 2000);
return deferred.promise;
}
};
var AboutCtrl = app.controller('AboutCtrl', function($scope, AboutCtrlData){
$scope.model = AboutCtrlData;
});
AboutCtrl.resolve = {
AboutCtrlData: function($q, $timeout){
var deferred = $q.defer();
$timeout(function(){
return deferred.resolve('AboutCtrlResolver');
}, 2000);
return deferred.promise;
}
};
在 jsfiddler 脚本中,当 HomeCtrl 想要解析 HomeCtrlData 我收到此错误 Unknown provider: HomeCtrlDataProvider <- HomeCtrlData
如果我在 HomeCtrl 中将 HomeCtrlData 更改为 AboutCtrlData 一切正常,似乎分配的唯一解析是 AboutCtrl.resolve
任何的想法?