Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //CONTROLLER
  2. var getSomeData = async function () {
  3.     $timeout(function () {
  4.         vm.isLoadingSomeData = true; //Flag for spinner in UI
  5.     });
  6.     try {
  7.         var someDataLoadRequestResult = await dataService.getSomeData();
  8.         //Handle result here
  9.     } catch (error) {
  10.         //Handle error here
  11.     }
  12.     $timeout(function () {
  13.         vm.isLoadingSomeData = false;
  14.     });
  15. };
  16.  
  17. vm.getSomeData= getSomeData; //Triggers by ng-click in view
  18.  
  19. //Data load service/factory
  20. (function () {
  21.  
  22.     "use strict";
  23.  
  24.     angular.module("dataService", []).factory("dataService", dataService);
  25.  
  26.     dataService.$inject = ["$http", "$httpParamSerializerJQLike"];
  27.  
  28.     function dataService($http, $httpParamSerializerJQLike) {
  29.  
  30.         var onSuccess = function (response) {
  31.             return response.data;
  32.         };
  33.  
  34.         var onError = function (response) {
  35.             return response.status >= 400 ? response.data : response;
  36.         };
  37.  
  38.         var getSomeData= function () {
  39.             return $http({
  40.                 method: "get",
  41.                 url: "/YOUR_PATH",
  42.                 timeout: 1500,
  43.                 data: angular.toJson({}),
  44.                 headers: {
  45.                 }
  46.             }).then(onSuccess, onError);
  47.         };
  48.  
  49.         return {
  50.             getSomeData: getSomeData,
  51.         };
  52.     };
  53.  
  54. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement