3

I have the following circular dependency:

            $http
            /   \
           /     \
          /       \
         /         \
LoginManager------Interceptor
 (service)         (factory)

This Circular dependency only emerged after I added the code for Interceptor.

Interceptor will call the logout function in LoginManager in case a certain response in intercepted.

From what I see, only solution is to move interceptor code inside the LoginManager service as an anonymous factory

Is there any better way?

2

1 Answer 1

6

You can avoid the circular dependency by using the injector service to get an instance of LoginManager at runtime.

var loginManager = $injector.get('LoginManager');

Just make sure you use this code inside one of the methods of the interceptor (e.g responseError) and not directly in your interceptor creation code.

Sign up to request clarification or add additional context in comments.

4 Comments

using $injector does not have any effect on the circular dependency.
Did you remove LoginManager from the dependency list of Interceptor?
my bad, the $injector.get thing needs to be inside the return object of the interceptor. I think it would be good if you mention it in the answer.
The last sentence in this answer saved my butt, none of the answers on SO, including the link in the comment section of the question make that clear. Thanks so much dude (I'm aware this comment is in bad form but w/e)

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.