[英]AngularJs promise returns undefined
我正在嘗試從AuthLoginController控制器使用AuthService工廠的登錄功能。 問題是當用錯誤的電子郵件和密碼執行User.login函數時,console.log()返回false ,這就是我想要的。 但是,當我使用正確的電子郵件和密碼時,
Error: response is not defined
我不明白,因為一切都可以通過function(error){}正常運行,但不能通過functionfunction(success){}正常運行,即使它們都是異步調用的結果。
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: 'test1@test.com',
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檢索true ? 謝謝(對不起,我的英語不好)
.then(function(success) {
$rootScope.currentUser = {
id: **response**.user.id,
tokenId: **response**.id,
email: email,
firstname: **response**.user.firstname,
lastname: **response**.user.lastname
};
return true;
},
未在“成功”回調中定義響應變量。 應該改為使用“成功”參數變量,或者應該將其重命名為響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.