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?

Ishan Khare
  • 1,745
  • 4
  • 28
  • 59
  • can you post your `interceptor` and `LoginManager` code? – iulian Mar 20 '16 at 20:49
  • Possible duplicate of [AngularJS: Injecting service into a HTTP interceptor (Circular dependency)](http://stackoverflow.com/questions/20647483/angularjs-injecting-service-into-a-http-interceptor-circular-dependency) – Léo Lam Mar 21 '16 at 18:28

1 Answers1

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.

bumpy
  • 2,002
  • 2
  • 14
  • 19
  • using `$injector` does not have any effect on the circular dependency. – Ishan Khare Mar 21 '16 at 11:24
  • Did you remove `LoginManager` from the dependency list of `Interceptor`? – bumpy Mar 21 '16 at 11:30
  • 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. – Ishan Khare Mar 21 '16 at 11:37
  • 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) – KreepN Aug 01 '16 at 14:16