AngularJS Promise : $http GET example
A simple example using $http.get() to discover its promises and theirs specific functions like success() or error()
HTML
<div ng-app="myModule">
<div ng-controller="PromiseCtrl">
<h2>$http.get</h2>
<p>{{example4}}</p>
<h3>Example 5 : Error callback with error()</h3>
<p>{{example5}}</p>
<h3>Example 6 : Error callback with catch()</h3>
<p>{{example6}}</p>
</div>
</div>
JavaScript
angular.module('myModule', []);
angular.module('myModule').service('ViewsService', ['$http', '$httpParamSerializer', ViewsService]);
angular.module('myModule').controller('PromiseCtrl', ['$scope', '$http', 'ViewsService', PromiseCtrl]);
function ViewsService($http, $httpParamSerializer){
var srv = {
test: test,
test2: test2
};
function test(){
console.log("views service test.");
$http({ method: "POST", url: '/echo/json/', data: $httpParamSerializer({
json: "text",
delay: 85
}), headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}}).then((res) => {
//res has data from first http
return srv.test2().then(
function(rsp){
console.log("success");
}, function(data){
console.log("error");
});
});
}
function test2(){
console.log("views service test2.");
return $http.get('http://fiddle.jshell.net/echo/json?x=4').then(
function(rsp){
console.log("success22");
}, function(data){
console.log("error22");
});
}
return srv;
}
function PromiseCtrl($scope, $http, ViewsService) {
ViewsService.test();
}