Skip to content

Instantly share code, notes, and snippets.

@sudodoki
Created September 19, 2013 21:25
Show Gist options
  • Select an option

  • Save sudodoki/6630060 to your computer and use it in GitHub Desktop.

Select an option

Save sudodoki/6630060 to your computer and use it in GitHub Desktop.
Angular Interceptors
// Set up ajax Interceptors
app.factory('myHttpInterceptor', ["$q", "$rootScope",function ($q, $rootScope) {
return {
'request': function(config) {
// do something on success
$rootScope.$broadcast('request')
console.log(config)
return config
},
// optional method
'requestError': function(rejection) {
// do something on error
$rootScope.$broadcast('requestError')
return $q.reject(rejection);
},
// optional method
'response': function(response) {
// do something on success
$rootScope.$broadcast('response')
return response
},
// optional method
'responseError': function(rejection) {
// do something on error
$rootScope.$broadcast('responseError')
return $q.reject(rejection);
}
}
}]);
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('myHttpInterceptor');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment