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', ViewsService]);
angular.module('myModule').controller('PromiseCtrl', ['$scope', '$http', 'ViewsService', PromiseCtrl]);
function ViewsService($http){
var srv = {
test: test,
test2: test2
};
function test(){
console.log("views service test.");
$http.get('http://fiddle.jshell.net/echo/json/error').then(
function(rsp){
console.log("success");
srv.test2();
}, function(data){
console.log("error");
});
}
function test2(){
console.log("views service test2.");
$http.get('http://fiddle.jshell.net/echo/json').then(
function(rsp){
console.log("success22");
}, function(data){
console.log("error22");
});
}
return srv;
}
function PromiseCtrl($scope, $http, ViewsService) {
$http.get('http://fiddle.jshell.net/echo/json').then(function(rsp) {
console.log("ok");
ViewsService.test();
$http.get('http://fiddle.jshell.net/echo/json/error').then(function(rsp){ console.log("rsp"); $http.get('http://fiddle.jshell.net/echo/json'); }, function(data, status) {
$scope.example6 = data.status;
});
}, function(data, status) {
console.log("ok3");
$scope.example5 = status;
});
}