如何在成功处理程序之外使用 $http 承诺响应 [英] How to use $http promise response outside success handler
问题描述
$scope.tempObject = {};$http({方法:'获取',url: '/myRestUrl'}).then(函数successCallback(响应){$scope.tempObject = 响应console.log("successCallback 中的临时对象", $scope.tempObject);}, 函数错误回调(响应){});console.log("$http 外的临时对象", $scope.tempObject);
我在 successCallback
中得到响应,但是在 $http
之外没有得到 $scope.tempObject
.它显示 undefined
.
如何在$http
response
或$scope.tempObject
但是如果我想在回调后使用 $scope.tempObject 那么我该如何使用它.?
您需要从 httpPromise 中链接.保存 httpPromise 并将值返回给 onFullfilled 处理函数.
//保存httpPromise用于链接var httpPromise = $http({方法:'获取',url: '/myRestUrl'}).then(function onFulfilledHandler(response) {$scope.tempObject = 响应console.log("successCallback 中的临时对象", $scope.tempObject);//返回链接对象返回 $scope.tempObject;});
然后从 httpPromise 外部链.
httpPromise.then (function (tempObject) {console.log("$http 外的临时对象", tempObject);});
有关更多信息,请参阅 并在数据从服务器到达并且 XHR 完成之后调用.
演示
console.log("Part 1");console.log("第二部分");var promise = new Promise(r=>r());承诺.然后(功能(){console.log("第三部分");});console.log("Part *4*");
其他资源
忍者小队 -- 陷阱、反模式和关于 AngularJS 承诺的技巧
很好的理论,但需要更新以使用.then
和.catch
方法.
$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);
I am getting response in successCallback
but
not getting $scope.tempObject
outside $http
. its showing undefined
.
How to access response
or $scope.tempObject
after $http
But if I want to use $scope.tempObject after callback so how can I use it. ?
You need to chain from the httpPromise. Save the httpPromise and return the value to the onFullfilled handler function.
//save httpPromise for chaining
var httpPromise = $http({
method: 'GET',
url: '/myRestUrl'
}).then(function onFulfilledHandler(response) {
$scope.tempObject = response
console.log("Temp Object in successCallback ", $scope.tempObject);
//return object for chaining
return $scope.tempObject;
});
Then outside you chain from the httpPromise.
httpPromise.then (function (tempObject) {
console.log("Temp Object outside $http ", tempObject);
});
For more information, see AngularJS $q Service API Reference -- chaining promises.
It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain. This makes it possible to implement powerful APIs.1
Explaination of Promise-Based Asynchronous Operations
console.log("Part1");
console.log("Part2");
var promise = $http.get(url);
promise.then(function successHandler(response){
console.log("Part3");
});
console.log("Part4");
The console log for "Part4" doesn't have to wait for the data to come back from the server. It executes immediately after the XHR starts. The console log for "Part3" is inside a success handler function that is held by the $q service and invoked after data has arrived from the server and the XHR completes.
Demo
console.log("Part 1");
console.log("Part 2");
var promise = new Promise(r=>r());
promise.then(function() {
console.log("Part 3");
});
console.log("Part *4*");
Additional Resources
What is the explicit promise construction antipattern and how do I avoid it?
Why are angular $http success/error methods deprecated? Removed from v1.6?
Ninja Squad -- Traps, anti-patterns and tips about AngularJS promises
Good theory but needs to be updated to use.then
and.catch
methods.
这篇关于如何在成功处理程序之外使用 $http 承诺响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!