Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs Circular Dependency for Interceptor

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?

like image 593
Ishan Khare Avatar asked Feb 28 '26 13:02

Ishan Khare


1 Answers

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.

like image 86
bumpy Avatar answered Mar 03 '26 11:03

bumpy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!