将$ modal注入httpProvider

时间:2015-03-17 15:39:48

标签: angularjs twitter-bootstrap

我试图将bootstrap $ modal注入httpProvider以显示模态中的内部服务器错误消息(500),但是我收到循环依赖性错误(找到循环依赖项:$ http< - $ modal< - $ http< - 区域)

app.config(function($httpProvider) {
$httpProvider.interceptors.push(function ($q, $location, $modal) {
    return {
        'response': function (response) {
            return response;
        },
        'responseError': function (rejection, modalInstance) {
            if(rejection.status === 401) {
                console.log('No permissions - redirected to the login page');
                $location.path('/login');
            } else if (rejection.status === 404) {
                var modalInstance = $modal.open({
                    templateUrl: 'templates/modal/error.tpl.html',
                    controller: 'ModalController',
                    size: 'md',
                    resolve: {
                        object: function () {
                            return object;
                        }
                    }
                });
            }
            return $q.reject(rejection);
        }
    };
});

任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:7)

使用$ injector:

app.config(function($httpProvider) {
$httpProvider.interceptors.push(function ($q, $location, $injector) {
  var modal = $injector.get("$modal");
  modal.open ...