Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CONTROLLER
- var getSomeData = async function () {
- $timeout(function () {
- vm.isLoadingSomeData = true; //Flag for spinner in UI
- });
- try {
- var someDataLoadRequestResult = await dataService.getSomeData();
- //Handle result here
- } catch (error) {
- //Handle error here
- }
- $timeout(function () {
- vm.isLoadingSomeData = false;
- });
- };
- vm.getSomeData= getSomeData; //Triggers by ng-click in view
- //Data load service/factory
- (function () {
- "use strict";
- angular.module("dataService", []).factory("dataService", dataService);
- dataService.$inject = ["$http", "$httpParamSerializerJQLike"];
- function dataService($http, $httpParamSerializerJQLike) {
- var onSuccess = function (response) {
- return response.data;
- };
- var onError = function (response) {
- return response.status >= 400 ? response.data : response;
- };
- var getSomeData= function () {
- return $http({
- method: "get",
- url: "/YOUR_PATH",
- timeout: 1500,
- data: angular.toJson({}),
- headers: {
- }
- }).then(onSuccess, onError);
- };
- return {
- getSomeData: getSomeData,
- };
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement