Tag: fix

Angular Circular Dependency

In an Angular (1.5) project I recently came across the following error:

16:12:17.669 Error: [$injector:cdep] Circular dependency found: AuthService <- RequestService <- AuthService
http://errors.angularjs.org/1.5.3/$injector/cdep?p0=AuthServic%20%3C-%20RequestService%20%3C-%20AuthService

In this case the problem was related to automatic dependency injection. My AuthService had a dependency on the RequestService to perform a login request. But the RequestService had a dependency on the AuthService to retrieve the accessToken for user-authenticated requests.

The fix was simple, using the $injector. I removed the RequestService dependency from the AuthService, and in the login() function in the AuthService I manually injected the RequestService:

 var RequestService = $injector.get('RequestService');

This bypassed the automatic injection and allowed me to keep using this setup.

Leave a Comment