How To Call Rest Service From Angularjs
-
- 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
- myApp.mill( 'myService' , [ '$http' , '$q' , function ($http, $q)
- {
- function getBookData()
- {
- var deferred = $q.defer();
- $http.get ( 'https://myRestServiceUrl' ).success(office (data)
- {
- deferred.resolve(data);
- }).error(function (err)
- {
- panel.log('Error retrieving data' );
- deferred.turn down(err);
- });
- return deferred.promise;
- }
- render {
- getBookData: getBookData
- };
- }]);
Utilize $http Service method into Controller
- var myApp = athwart.module( 'myApp' , []);
- myApp.controller('myCtrl' , [ '$telescopic' , 'myService' , role ($scope, myService)
- {
- function fetchBookDetails()
- {
- $scope.isLoading =true ;
- myService.getBookData().then(office (information)
- {
- $scope.bookDetails = data.books;
- });
- }
- fetchBookDetails();
- $scope.getBookDataDetails = function ()
- {
- fetchBookDetails();
- };
- }]);
$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:
- resolve:
It resolves the derived promise.
Case:
- .success(function (data) {
- deferred.resolve(data);
- })
- turn down:
It rejects the derived promise with the reason $q.turn down.
Example:
- .error(office (err) {
- console.log('Error retrieving data' );
- deferred.pass up(err);
- });
- promise:
Information technology returns hope after resolving the service response.
Example:
- 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