AngularJs $http Service

$http Service communicate with the $http servers via the browser XMLHttpRequest object or via JSONP. $http service encapsulates HTTP communication, Its a method of the object or GET,POST, PUT,DELETE. $http request always return a promise. A promise to deliver a value in the future promise called a then method in the future , then method call my function that data is ready.

by santhosh lanka

HTML

<div ng-app="app" ng-controller="mainCtrl">
  <div>
    <div>{{error}}</div>
    <div>{{user.name}}</div>
    <div><img src="{{user.avatar_url}}" alt=""></div>
  </div>
</div>

JavaScript

angular.module("app", [])
	.controller("mainCtrl", function($scope,$http){
    var onUserComplete = function(response){
    	$scope.user = response.data;
    };
    
    var onError = function(reason){
    	$scope.error = "Could not fetch the user";
    }
    
    $http.get("https://api.github.com/users/angular")
    	.then(onUserComplete, onError);
  });