$location from $exceptionHandler - dependency conflict

╄→гoц情女王★ 提交于 2020-01-11 21:19:10

问题


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

In a simplified form the code looks like this:

app.factory('$exceptionHandler', ['$location', function($location) {
    return function(exception, cause) {
        $location.path("/error");
    };
}]);

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

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

But how else can we do it then?


回答1:


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');
        $location.path("/error");
    };
}]);


来源:https://stackoverflow.com/questions/19554624/location-from-exceptionhandler-dependency-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!