I have a service called 'api', registered something like this:
angular .module('myApp') .factory('api', ['$http', function ($http) { // do stuff with $http.get() etc here. }]);
... and $http is being customized like this elsewhere:
angular .module('myApp') .factory('httpInterceptor', ['$rootScope', function ($rootScope) { // do stuff to intercept http requests and auth things here }]); angular .module('myApp') .config(function ($httpProvider) { $httpProvider.interceptors.push('httpInterceptor'); });
The interceptors are working when I directly inject $http
into a controller, but when I use the api
service in my controller, the $http
customizations are not working. Looks like Angular does not add the intercepts by the point it's required by the factory I created.
How do I solve this?
The question was based on a wrong premise. The interceptors are in fact working, I was just using the wrong hook point. Instead of customizing the responseError
property of the interceptor object, I was trying to intercept errors from the response
property itself. This is why I thought the interceptors weren't working at all.
This problem does not really exist. The provider's interceptors are working correctly even in a factory.