我的服務是:
myApp.service('userService', [
'$http', '$q', '$rootScope', '$location', function($http, $q, $rootScope, $location) {
var deferred;
deferred = $q.defer();
this.initialized = deferred.promise;
this.user = {
access: false
};
this.isAuthenticated = function() {
this.user = {
first_name: 'First',
last_name: 'Last',
email: '[email protected]',
access: 'institution'
};
return deferred.resolve();
};
}
]);
我通過呼叫這在我的config
文件:
myApp.run([
'$rootScope', 'userService', function($rootScope, userService) {
return userService.isAuthenticated().then(function(response) {
if (response.data.user) {
return $rootScope.$broadcast('login', response.data);
} else {
return userService.logout();
}
});
}
]);
然而,抱怨說then
不是一個函數。我不是在回覆已解決的承諾嗎?
@BenjaminGruenbaum是的,你是正確的,我不知道'$超時'實際上返回一個承諾,serviceMethod可以縮短爲:返回$超時(函數(){返回{屬性:'值'};},1000); – Brocco
這可能會起作用,但它肯定不是推薦的方法來返回承諾 –
@BenjaminGruenbaum我做了,現在返回$超時產生的承諾。 – Brocco