我有我的authservice下面给出,承诺返回对象的角度JS
myApp.factory('Authentication',
['$rootScope', '$location', 'URL', '$http', '$q',
function ($rootScope, $location, URL, $http, $q) {
var myObject = {
authwithpwd: function (user) {
var dfd = $q.defer();
$http
.post('Mart2/users/login', {email: user.email, password: user.password})
.then(function (res) {
return dfd.resolve(res.data);
}, function (err) {
return dfd.reject(err.data);
});
return dfd.promise;
} //login
};
return myObject;
}]); //factory
而且我使用用户服务,服务如下:
myApp.factory('UserService',
['$rootScope', '$location', 'URL', '$http', '$q', 'Authentication',
function ($rootScope, $location, URL, $http, $q, $Authentication) {
var myObject = {
login: function (user) {
$Authentication.authwithpwd(user).then(function (regUser) {
console.log(regUser);
}).catch(function (error) {
$rootScope.message = error.message;
});
},
getUserToken: function() {
return $rootScope.currentUser.apiKey;
},
isLogged: function() {
if ($rootScope.currentUser) {
return true;
} else {
return false;
}
}
//login
};
return myObject;
}]); //factory
我是很新的角度js。当编写服务和从控制器调用该服务时,我已经把控制台调试放在显示其返回对象的用户服务中。我得到的对象如果我做console.log(regUser)?任何想法为什么?
你能澄清你的问题吗?什么是你的问题?你没有得到myObject中的返回对象吗?小提琴或plunkr也会有所帮助 –
我没有从AuthenticationService的用户服务的登录方法中获得返回的承诺。我得到[对象,对象]在我已经完成console.log(regUser)。我将尝试为此创建一个小提琴 –
避免使用$ rootscope - > http://stackoverflow.com/questions/36645399/save-current-user-information-and-use-it-in-controllers/ 36645460?noredirect = 1#comment60884162_36645460 – thegio