Chicken and egg issue with dependency injection in angular, $exceptionHandler and $http
Asked Answered
G

1

0

I'm enjoying a nice chicken and egg problem with dependancy injection in angular, I override $exceptionHandler by injecting $http, but I also have a custom interceptor.

Uncaught Error: [$injector:unpr] Unknown provider:  $httpProvider <-  $http <- $exceptionHandler <- $rootScope

I am trying to use it like so:

Module.config(['$httpProvider', function($httpProvider) {
  $httpProvider.interceptors.push('special-http');
}]);

I wrapped my interceptor in a factory:

Module.factory('special-http', ['$templateCache', function($templateCache) {
  "use strict";

  return {
    'request': function(config) {

      if ($templateCache.get(config.url)){
        return config;
      }

     //some logic

      return config;
    }
  }
}]);

exceptionHandler.js

Module.factory('$exceptionHandler', [' $http', function ($http) {
    "use strict";
//code
}
Gravedigger answered 27/3, 2014 at 9:56 Comment(3)
please add jsfiddle or full code example.Takin
My magical crystal ball is telling me that you forgot to inject $http whereever the heck you wanted it to useMisogamy
@AlexChoroshin its a massive app so not really, it was working before in the default and expect wayGravedigger
T
1

Here's a working example of creating interceptors .

Example:

app.config(['$httpProvider', function($httpProvider) {
            $httpProvider.interceptors.push(function($q) {
            return {
             'request': function(config) {
                 // same as above
              },

              'response': function(response) {
                 // same as above
              }
            };
          }); 
    }]);

Live example: http://jsfiddle.net/choroshin/mxk92/

Takin answered 27/3, 2014 at 10:35 Comment(2)
I think mine should work the same, can you check my edit. This code was working before but for some reason deploying it on a new server is causing this issue..Gravedigger
your code is the same as mine and probably some server errorTakin

© 2022 - 2024 — McMap. All rights reserved.