[英]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.