$ exceptionHandler中的$ location-依赖冲突 [英] $location from $exceptionHandler - dependency conflict

查看:70
本文介绍了$ exceptionHandler中的$ location-依赖冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行一个非常标准的任务:发生异常时,请重定向到我的/error页面.

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");
    };
}]);

但是,AngularJS抱怨: 找到循环依赖项:$ location<-$ exceptionHandler<-$ rootScope

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

这似乎是一个基本限制,不允许在处理异常时使用$location.

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

但是我们还能怎么做呢?

But how else can we do it then?

推荐答案

要解决此问题,您需要手动调用$injector以便在运行时解决依赖项:

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");
    };
}]);

这篇关于$ exceptionHandler中的$ location-依赖冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆