Is it possible to check condition before all http calls in Angular?

空扰寡人 提交于 2019-12-11 07:23:29

问题


Is it possible to check if: $rootScope.variable is TRUE

Before all $http calls are called, or I should check for every individual call? Some of my calls are call through angular factory, and some are not.

I thought there is maybe some way like httpInterceptor which would check before some call is ran.

Any help would appreciated.


回答1:


You can create an interceptor for this issue like this one:

angular.module('myModule', []).config(function($httpProvider) { 
    $httpProvider.interceptors.push(function($rootScope) {
        return {
            'request': function(config) {
                if($rootScope.yourVariable) {
                    // cancel this request
                }
            }
        }
    });
})

This interceptor processes every request. You can find an implementation for cancelling requests here




回答2:


To further @boindiil's answer. I tend to have it like this:

angular.module('someModule', [])
    .factory('httpInterceptorService', ['$q', httpInterceptorService])
    .config(['$httpProvider', interceptorConfig]);

function httpInterceptorService($q) {
    var service = {
        request: request,
        responseError: responseError
    };

    return service;

    function request(config) {
        // do some logic
        return config;
    }

    function responseError(rejection) {
        if (rejection.status === 401) {
             // they were unauthorised.
        }

        return $q.reject(rejection);
    }
}

function interceptorConfig ($httpProvider) {
    $httpProvider.interceptors.push('httpInterceptorService');
}

Here it is more separated out. And you can see how you can easily push more interceptors into the pipeline. Obviously you can inject what ever you like into the httpInterceptorService like the $rootScope.

Just beware not create any circular dependencies.

I like what @pankajparkar commented about, maintaining a proper call stack.

You can do this instead of using interceptors (as they are for every request).

angular.module('someModule', [])
    .factory('mainService', ['$http', '$rootScope', '$q', mainService])
    .controller('MainCtrl', ['mainService', mainCtrl]);

function mainService($http, $rootScope, $q) {
    var service = {
        getThings: getThings
    };

    var serviceBase = '/Api/Things';

    return service;

    function getThings() {
        var deferred = $q.defer();

        $http.get(serviceBase).then(function (data) {
            if (data.data.someVariable == $rootScope.someVariable) {
                deferred.resolve(data.data);
            } else {
                deferred.reject(data);
            }
        }).catch(function (message) {
           deferred.reject(message);
        });

        return deferred.promise;
    }
}

function mainCtrl(mainService) {
    var vm = this;

    vm.httpData = {};

    mainService.getThings().then(function (data) {
        vm.httpData = data;
    }, function (message) {
        // do something with the error.
    });
}


来源:https://stackoverflow.com/questions/28713753/is-it-possible-to-check-condition-before-all-http-calls-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!