访问promise对象的值angular

时间:2016-08-17 03:35:04

标签: javascript angularjs promise angular-promise

var requestOne = $http({
                    method: 'POST',
                    url: $rootScope.apisrvr + 'user/user_signin',
                    data: { username: $scope.user.username, password: $scope.user.password },
                });

这是我的代码,当我写:console.log(requestOne);我得到: that

我只想得到$$state / value / data / salt。但是,当我尝试console.log(requestOne.$$state.value.data.salt);时,我收到错误TypeError: Cannot read property 'data' of undefined。我如何从这里获得salt

1 个答案:

答案 0 :(得分:1)

requestOne是promise变量本身,要实际获得结果,您需要在promise的末尾添加.then调用,如下所示:

var requestOne = $http({
                    method: 'POST',
                    url: $rootScope.apisrvr + 'user/user_signin',
                    data: { username: $scope.user.username, password: $scope.user.password },
                }).then(function(result){
                    console.log(result);
                });