Created
September 19, 2013 21:25
-
-
Save sudodoki/6630060 to your computer and use it in GitHub Desktop.
Angular Interceptors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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