banner



How To Call Rest Service From Angularjs

  • Gul Md Ershad
  • Updated date Feb 24, 2016
  • 10.2k
  • 1

This web log is intended to explain the concept of rest service telephone call in AngularJS.

IntroductionAngularJs provides $http for the advice withthe endpoint by calling Restful endpoint. It issues XHR and JSONP calls. It uses several methods like GET, Post, PUT, DELETE, Head, etc.

The $http.GET or $http.POST method accepts any JavaScript object or a string value as the data parameter. It converts the JavaScript data object into JSON String.

Residue Service Call using With $http

  1. myApp.mill( 'myService' , [ '$http' , '$q' , function ($http, $q)
  2. {
  3.     function getBookData()
  4.     {
  5.         var deferred = $q.defer();
  6.         $http.get ( 'https://myRestServiceUrl' ).success(office (data)
  7.         {
  8.             deferred.resolve(data);
  9.         }).error(function (err)
  10.         {
  11.             panel.log('Error retrieving data' );
  12.             deferred.turn down(err);
  13.         });
  14. return  deferred.promise;
  15.     }
  16. render  {
  17.         getBookData: getBookData
  18.     };
  19. }]);

Utilize $http Service method into Controller

  1. var myApp = athwart.module( 'myApp' , []);
  2. myApp.controller('myCtrl' , [ '$telescopic' , 'myService' , role ($scope, myService)
  3. {
  4.     function fetchBookDetails()
  5.     {
  6.         $scope.isLoading =true ;
  7.         myService.getBookData().then(office (information)
  8.         {
  9.             $scope.bookDetails = data.books;
  10.         });
  11.     }
  12.     fetchBookDetails();
  13.     $scope.getBookDataDetails = function ()
  14.     {
  15.         fetchBookDetails();
  16.     };
  17. }]);

$q

$q is a service that helps to run functions asynchronously and information technology returns value whenan asynchronous task completes the process.

$q includes instances or featurs in the form of $q.defer():

It exposes the associated hope instance as well as APIs that can be used for signaling the successful or unsuccessful completion.

The instance of $q.defer() includesthe  beneath methods:

  1. resolve:

    It resolves the derived promise.

    Case:

    1. .success(function (data) {
    2.    deferred.resolve(data);
    3. })
  2. turn down:

    It rejects the derived promise with the reason $q.turn down.

    Example:

    1. .error(office (err) {
    2.    console.log('Error retrieving data' );
    3.    deferred.pass up(err);
    4. });
  3. promise:

    Information technology returns hope after resolving the service response.

    Example:

    1. render  deferred.promise;

How To Call Rest Service From Angularjs,

Source: https://www.c-sharpcorner.com/blogs/call-rest-service-in-angularjs

Posted by: carterancralows1973.blogspot.com

0 Response to "How To Call Rest Service From Angularjs"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel