1

I'm having a problem trying to inject $ http to the factory. I miss the following error in Angular 1.6:

Circular dependency found: $rootScope <- $http <- $exceptionHandler <- $rootScope <- $route

And this was what I was doing so far:

var app = angular.module("app", []);
app
  .controller("ctrl", function($scope) {
    // controller
  })
  .factory('$exceptionHandler', ['$log', '$http', function($log, $http) {
    return function myExceptionHandler(exception, cause) {
      $log.warn(exception, cause);
      // I show only this and it already throws error
      console.log($http);
    }
  }]);

1 Answer 1

2

To work around the circular dependency, try the following.

Instead of injecting $http directly into the interceptor try injecting the $injector and use that directly to get $http.

var app = angular.module("app", []);
app.factory('$exceptionHandler', ['$log', '$injector', function($log, $injector) {
    return function myExceptionHandler(exception, cause) {
        var $http = $injector.get('$http');
        $log.warn(exception, cause);
        // I show only this and it already throws error
        console.log($http);
    }
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

@Kleith If the error persists, more information is required. Bulkan's solution above cannot cause a circular dependency error, based on what we know so far.
It's just the same error Circular dependency found that i describe in the question.
I miss the [], my bad. It works fine but when i tried something else this crash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.