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="app">
<div ng-controller="PromiseCtrl">
<h2>$http.get()</h2>
<h3>Example 7 : Success callback followed by finally()</h3>
<p>{{example7}}</p>
<h3>Example 8 : Error callback followed by finally()</h3>
<p>{{example8}}</p>
</div>
</div>
JavaScript
var app = angular.module('app', []);
app.controller('PromiseCtrl', function($scope, $http) {
$http.get('http://localhost:9000/comments?number=10')
.then(function(value) {
$scope.example7 = value.status;
})
.catch(function() {
$scope.example7 += " (catch called)";
})
.then(function(value) {
$scope.example7 += " (then called again)";
return Promise.reject('err'); // NOTHING TO CATCH THIS ONE
}, function() {
$scope.example7 += " (catch called again)";
})
.finally(function() {
$scope.example7 += " (Finally called)";
});
$http.get('/echo/json')
.then(function(value) {
$scope.example8 = value.status;
})
.catch(function() {
$scope.example8 += " (catch called)";
})
.finally(function() {
$scope.example8 += " (Finally called)";
});
});