【发布时间】:2017-02-07 21:34:22
【问题描述】:
我正在尝试从我的 AuthLoginController 控制器使用我的 AuthService 工厂的登录功能。问题是当使用错误的电子邮件和密码执行 User.login 函数时,console.log() 返回 false 这是我想要的。但是当我使用正确的电子邮件和密码时,我会收到一个
Error: response is not defined
我不明白,因为使用 function(error){ } 一切正常,但使用 function(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不需要返回一个承诺吗? -
嗯,我不知道。我正在努力使用 AngularJs。我读过调用 promise 的 then 方法会返回一个新的派生 promise,但我不知道应该使用哪种方法。
标签: javascript angularjs promise angular-promise