Skip to content

Instantly share code, notes, and snippets.

@kylebuch8
Created July 20, 2015 23:48
Show Gist options
  • Save kylebuch8/076ca81676f0a72699cb to your computer and use it in GitHub Desktop.
Save kylebuch8/076ca81676f0a72699cb to your computer and use it in GitHub Desktop.
AngularJS httpInterceptor
config.$inject = ['$provide', '$httpProvider'];
function config($provide, $httpProvider) {
$provide.factory('httpInterceptor', httpInterceptor);
httpInterceptor.$inject = ['$rootScope'];
function httpInterceptor($rootScope) {
return {
'request': request,
'response': response,
'responseError': responseError
};
function request(config) {
$rootScope.loading = true;
return config;
}
function response(responseObj) {
$rootScope.loading = false;
return responseObj;
}
function responseError(rejection) {
$rootScope.loading = false;
return rejection;
}
}
$httpProvider.interceptors.push(httpInterceptor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment