John Liu .NET

View Original

AngularJS $http and logger Circular Dependency

Error

[$injector:cdep] Circular dependency found: $rootScope <- $http <- logger <- $exceptionHandler <- $rootScope <- $location <- routehelper

TLDR:

If you are looking for RequireJS's easy way to late-bind a dependency, here's how you do it in AngularJS.

See this content in the original post

Story

I imagine this could be a fairly common scenario.

You implemented AngularJS, with a logger service.

See this content in the original post

You want to try to catch errors and log it back to your webservice
So add a REST call.

See this content in the original post

Error!

[$injector:cdep] Circular dependency found: $rootScope <- $http <- logger <- $exceptionHandler <- $rootScope <- $location <- routehelper

I actually quite like this error.  It's telling me the dependency tree starting from routehelper and looping around to $rootScope

Bingle will tell you to re-architect your solution so you have proper separation of concerns.  But to be honest, I just want to log an error…

So here's how you do it.

See this content in the original post

Final Code Summary

See this content in the original post