vitaly-t vitaly-t - 1 year ago 136
AngularJS Question

$location from $exceptionHandler - dependency conflict

I'm trying to implement a very standard task: when an exception occurs, redirect to my


In a simplified form the code looks like this:

app.factory('$exceptionHandler', ['$location', function($location) {
return function(exception, cause) {

However, AngularJS complains:
Circular dependency found: $location <- $exceptionHandler <- $rootScope

This looks like a fundamental limitation, not to allow use of
when handling exceptions.

But how else can we do it then?

Answer Source

To get around this you need to call the $injector manually to resolve the dependency at runtime:

app.factory('$exceptionHandler', ['$injector', function($injector) {

    var $location;

    return function(exception, cause) {
        $location = $location || $injector.get('$location');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download