angular Circular Dependency
So, angular is/has a dependency injection mechanism.
It means, that it first loading the definition of all dependencies, and then instantiate and inject them on demand (angular has lazy instantiation).
Let’s have a look at this code:
Well, this code will obviously get us:
Error: $injector:cdep -> Circular Dependency
Le’ts explain the obvious:
When angular is trying to load service A, it goes and tries to load service B, but then it tries to load service A again, and here we have our circular dependency.
Solution: use $injector
In order to solve this, we can use the $injector explicitly in service B:
Why is this working? didn’t we call the injector anyway?
The answer is: No.
The $injector is trying to load service A, only at the “run” block, long after all service definitions have been loaded.
Now seriously…
While using $injector explicitly might have some good reasons (though I doubt it), this is clearly not one of them.
Please, don’t be afraid to refactor: