0

Do you know if it's possible to use angular services inside each other? I tried it but getting error "Circular dependency found". For example I have two services A and B. Can I use service A inside service B and service B inside service A ?

1 Answers1

0

No , you cant do it with basic angularjs dependency injector, but you can retrieve any service with $injector

https://docs.angularjs.org/api/auto/service/$injector

so can "lazy" load it to any var, and access it in other service.... but you should post some code.

Also it is smell like you are anti patterning something , or you have bad architecture when you have circular dependencies. Its like on the road :

Assume you have 2 Services

Foo Depends on Bar
Bar depends on Foo

Angular will go as this

Foo -> Depends on bar so load Bar -> Instantiate Bar but its depends on Foo so instantiate Foo -> Instantiate Foo but it depends on Bar but wait! Bar is still not instantiated because it waiting for Foo , so its cyclic.

You should have service

Foo with no dependency
Bar with no dependency

and Third factory

FooBar which will handle functions which needs the circular dependency.
Milos Mosovsky
  • 2,913
  • 1
  • 16
  • 18