$http.get()
services are used to get the promises of your services and there is no need to
use the "then" in services. It only use in controllers.
In other ways, we can say that GET is
used to gets the existing data's from servers to the given URI i.e.
The examples as,
//AngularJs code sample var app = angular.module('myServiceApp', []); app.factory('myfactService', function ($http) { return { myServices: function () { return $http.get('api.json'); //this returns promise } }; }); app.controller('myServiceCtrl', function (myfactService, $scope) { $scope.result = null; //default is null //Call the services myfactService.myServices().then(function (rest) { if (rest) { if (rest.data) $scope.result = rest.data; } }); }); //HTML code-sample <div ng-app="myServiceApp"> <div ng-controller="myServiceCtrl"> Output Reuslt: {{result}} <br /> Reuslt at 0 index: {{result[0]}} <br /> Reuslt at 1 index: {{result[1]}} <br> </div> </div> //The live demo code sample(HTML + Angular) <!doctype html> <html> <head> <meta charset="utf-8"> <title>Processing $http response in service</title> <script src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script> <script> var app = angular.module('myServiceApp', []); app.factory('myfactService', function ($http) { return { myServices: function () { return $http.get('api.json'); //this returns promise } }; }); app.controller('myServiceCtrl', function (myfactService, $scope) { $scope.result = null; //default is null //Call the services myfactService.myServices().then(function (rest) { if (rest) { if (rest.data) $scope.result = rest.data; } }); }); </script> </head> <body ng-app="myServiceApp"> <div ng-controller="myServiceCtrl"> Output Reuslt: {{result}} <br /> Reuslt at 0 index: {{result[0]}} <br /> Reuslt at 1 index: {{result[1]}} <br> </div> </body> </html>
The live output as,
Blogger Comment
Facebook Comment