AngularJS Promise : $http GET example
A simple example using $http.get() to discover its promises and theirs specific functions like success() or error()
by mobradio
HTML
<div ng-app>
<div ng-controller="PromiseCtrl">
<h2>$http.get()</h2>
<h3>Example 1 : The promise object returned by $http.get()</h3>
<p>{{example1}}</p>
<h3>Example 2 : Success callback with then()</h3>
<p>{{example2}}</p>
<h3>Example 3 : Error callback with then()</h3>
<p>{{example3}}</p>
<h3>Example 4 : Success callback with success()</h3>
<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>
<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
function PromiseCtrl($scope, $http) {
$scope.example1 = $http.get('http://api.homolog.mobradio.com.br/v1/app/getAll?key=cd34d00e57d257bdd432cddb2932704c1e2');
$http.get('http://api.homolog.mobradio.com.br/v1/app/getAll?key=cd34d00e57d257bdd432cddb2932704c1e2').then(function(value) {
$scope.example2 = value;
});
$http.get('/echo/json/error').then(null, function(value) {
$scope.example3 = value.status;
});
$http.get('/echo/json').success(function(data, status, headers, config) {
$scope.example4 = status;
});
$http.get('/echo/json/error').error(function(data, status, headers, config) {
$scope.example5 = status;
});
$http.get('/echo/json/error').catch(function(value) {
$scope.example6 = value.status;
});
$http.get('/echo/json').then(function(value) {
$scope.example7 = value.status;
}).finally(function() {
$scope.example7 += " (Finally called)";
});
$http.get('/echo/json/error').then(null, function(value) {
$scope.example8 = value.status;
}).finally(function() {
$scope.example8 += " (Finally called)";
});
}