AngularJS修改拦截器内的$ scope

时间:2016-03-08 04:48:21

标签: angularjs

如何修改拦截器内的$scope变量?

var portal = angular.module('portal', ['ngResource', 'ui.utils', 'ngRoute']).config(function($httpProvider) {
    $httpProvider.interceptors.push(function($q) {
        return {
            responseError: function(rejection) {
                if(rejection.status == -1) {
                    // i want to modify this variable to true / false
                    $scope.restApiServerOnline = false;
                    return;
                }
                return $q.reject(rejection);
            }
        };
    });
});

上述代码的目的是拦截对REST服务器的所有HTTP post / get,如果失败,则切换此范围变量。

1 个答案:

答案 0 :(得分:3)

我会在配置注入中包含function($httpProvider, $rootScope) { ,例如:

$rootScope.restApiServerOnline = false;

然后根据变量的上下文设置类似{{1}}的内容,无论如何,您似乎想要全局访问它。

相关问题