I want to use Angular's $http
but I'm not sure what to do with what it returns.
The main thing that is troubling me is that it returns an HttpPromise
. What do I do with that? How does it work?
I want to use Angular's $http
but I'm not sure what to do with what it returns.
The main thing that is troubling me is that it returns an HttpPromise
. What do I do with that? How does it work?
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
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);
}
}