如何角HttpPromises工作? [英] How do Angular HttpPromises work?
问题描述
我想用角的 $ HTTP
,但我不知道该怎么用它做返回。
I want to use Angular's $http
but I'm not sure what to do with what it returns.
这是困扰我的主要事情是,它返回一个 HttpPromise
。我该怎么跟那怎么办?它是如何工作的?
The main thing that is troubling me is that it returns an HttpPromise
. What do I do with that? How does it work?
推荐答案
这真的很简单。角的 $ HTTP
返回一个承诺,这就像任何其他类型的承诺,并且我建议你上的角的 $ q
It's really simple. Angular's $http
returns a Promise, which is like any other kind of Promise and I recommend you read up on Angular's $q
在下面的jsfiddle中, HttpPromise
是它的执行后,用了几秒钟。换句话说, $ http.get(...)
发出,但我们只做些事情的结果后,在我们的闲暇。
In the following JSFiddle, the HttpPromise
is used a few seconds after it's executed. In other words, $http.get(...)
is issued, but we only do something with the result later, at our leisure.
function Ctrl($scope, $http, $timeout) {
$scope.text = "text";
$scope.clicky = function(){
var httpPromise = $http.get('/echo/json/');
$timeout(function(){
httpPromise.success(function(data){
$scope.text = "Http Post Worked Succesfully";
console.log(data);
});
}, 3000);
}
}
这篇关于如何角HttpPromises工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!