What is the best method to use a $http service in angular JS? How can we achieve the promise paradigm and do the best Any help would be appreciated.
I have gone thru the API developer's guide and found the following..
$http.get(url)
I was wondering what would the promise call would look like and how do i attach a callback function to this??
Any call to $http returns a promise object, so you can then call the success method or error like this:
$http.get("/url").success(function(data){
})
.error(function(errorData, errorStatus){
})
or you can do this:
var promise = $http.get("/url");
promise.success(...)
Make sure you read about $resource, $q and $http to cover the "angular" way.
The best way to implement a callback is something like this
$http(url).then( function (){
return //values
})
Now if you want to achieve the promise paradigm you need to have a resolve config in your controller
resolve: {
value1: return $http.get(url).then( function (response){
if(response)
return response.data['value'];
})
}