0

I tried to use a factory in my Request Interceptor but i got a circular dependency error. I don't understand everything on AngularJS but i've understood that the injector is trying to get a service who's depending on itself.

My factory "Global" is used to make some HTTP request and display error messages on screen.

Here is how i'm trying to import the factory :

app.factory("authInterceptor", authInterceptor);

authInterceptor.$inject = ["$q","Global"];
function authInterceptor($q,Global) {
    return {

        // Add an interceptor for any responses that error.
        'responseError': function (response) {

            // Check if the error is auth-related.
            if (response.status === 401 || response.status === 403) {
              Global.show_fail("Connection expired, please authenticate again");
            }

            return $q.reject(response);
        }

    };

}


app.config(["$httpProvider",
    function ($httpProvider) {
        //Registers the interceptor
        $httpProvider.interceptors.push("authInterceptor");
    }]);

And here is the error on angularjs.org

Circular dependency found: $http <- Global <- authInterceptor <- $http <- $templateRequest <- $compile

And here is what service i use in global.js :

app.factory('Global', ['$rootScope', '$http','$mdDialog','$timeout', function($rootScope, $http, $mdDialog,  $timeout){ ... }

There is any reason for this error ?

Thanks in advance.

EDIT :

I've deleted $http service from my global factory and deported my request in another file.

Now i got

$templateRequest <- $$animateQueue <- $animate <- $$interimElement <- $mdDialog <- Global <- authInterceptor <- $http <- $templateRequest <- $compile

But i don't understand I've never call this $templateRequest service. Any idea ?

1 Answers1

1

The problem is that $httpProvider is a provider for $http service and you told angular to inject Global factory into the custom interceptor, but your Global factory itself depends on $http service. That's why it's a circular dependency

Dmitriy
  • 96
  • 1
  • 10