Tuesday, July 7, 2015

$http service in AngularJS

$http is an important service in AngularJS which helps in making Ajax calls to the server, $http takes the service URL to be invoked as a parameter, makes calls to the server gets the response and service and passes it back to Angular

The syntax for making Ajax calls using $http is as follows

  success(function(data, status, headers, config) {
          // Process response for Success here
  error(function(data, status, headers, config) {
          // Process response for Failure here

$http supports Get, Post and Delete operations, all these take the service URL as parameters, the Post variant in addition takes the Post data which is to be submitted to the service.

The following example shows how to make an API call using $http and display the results in the view using ng-repeat directive.

<html ng-app>
    <meta charset="utf-8" />
    <title>AngularJS - Basic</title>
    <script src="angular.min.js"></script>
                function httpAjaxController($scope,$http){
                       $scope.Title = "Package List";
            .then(function (results) {
                try {
                    $scope.packageList = results.data;
                catch (err) {
            }, function (results) {
                alert("Error: " + results.data + "; "
                                      + results.status);
          <div ng-controller="httpAjaxController">
                       <li ng-repeat="package in packageList">
                         {{ package.PackageId }} - {{ package.Name }}<br/>

Search Flipkart Products:

No comments: