我想從我的AuthLoginController控制器使用我的AuthService工廠的登錄功能。問題是,當使用錯誤的電子郵件和密碼執行User.login函數時,console.log()返回false這正是我想要的。但是當我使用了正確的電子郵件和密碼,我得到一個AngularJs承諾返回undefined
Error: response is not defined
我不明白這一點,因爲一切工作正常功能(錯誤){},但不與功能(成功){}即使他們都是異步調用的結果。
angular
.module('app')
.factory('AuthService', ['Member', '$q', '$rootScope', '$state', function(
User, $q, $rootScope, $state) {
function login(email, password) {
return User
.login({email: email, password: password})
.$promise
.then(function(success) {
$rootScope.currentUser = {
id: response.user.id,
tokenId: response.id,
email: email,
firstname: response.user.firstname,
lastname: response.user.lastname
};
return true;
},
function(error) {
return false;
}
);
} return {
login: login
};
}]);
這是我的AuthLoginController控制器。
angular
.module('app')
.controller('AuthLoginController', ['$scope', 'AuthService', '$state',
function($scope, AuthService, $state) {
$scope.user = {
email: '[email protected]',
password: 'test1'
};
$scope.login = function() {
AuthService.login($scope.user.email, $scope.user.password)
.then(function(response) {
console.log(response);
return;
}
//go to the default state after login
$state.go('test');
});
};
}]);
我如何可以檢索從我AuthLoginController 真正? 感謝 (對不起我的英文不好)
難道你不需要返回一個使用'then'的承諾嗎? – jdmdevdotnet
我不知道。我正在與AngularJs鬥爭。我讀過,調用promise的then方法會返回一個新的派生承諾,但我不知道應該使用哪種方法。 – ThomasF62