【发布时间】:2016-02-08 17:16:45
【问题描述】:
$scope.tempObject = {};
$http({
method: 'GET',
url: '/myRestUrl'
}).then(function successCallback(response) {
$scope.tempObject = response
console.log("Temp Object in successCallback ", $scope.tempObject);
}, function errorCallback(response) {
});
console.log("Temp Object outside $http ", $scope.tempObject);
我收到了successCallback 的回复,但是
在$http 之外没有得到$scope.tempObject。它显示undefined。
$http之后如何访问response或$scope.tempObject
【问题讨论】:
-
$http get 是一个异步调用。成功回调之外的 console.log 执行的时间,不执行成功回调。这就是 $scope.tempObject 未定义的原因。
-
但是如果我想在回调后使用
$scope.tempObject,我该如何使用它。 ? -
我建议学习 Promises。本文将帮助您了解它的工作原理 - pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html
-
你可以在回调函数中做这些事情。为什么在外面需要 $scope.tempObject?
-
@varun 我在外面有一些数据,所以我想在这个函数之外使用成功回调
response的数据
标签: angularjs angular-promise angular-http