Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $rootScope在http侦听器响应方法中未定义_Angularjs_Typescript - Fatal编程技术网

Angularjs $rootScope在http侦听器响应方法中未定义

Angularjs $rootScope在http侦听器响应方法中未定义,angularjs,typescript,Angularjs,Typescript,我有一个拦截器,我正在配置它来加载ajax微调器 interface IInterceptorScope extends angular.IRootScopeService { loading: number; } export class Interceptor { public static Factory($q: angular.IQService, $rootScope : IInterceptorScope) {

我有一个拦截器,我正在配置它来加载ajax微调器

interface IInterceptorScope extends angular.IRootScopeService {
        loading: number;
    }

    export class Interceptor {

        public static Factory($q: angular.IQService, $rootScope : IInterceptorScope) {
            return new Interceptor($q, $rootScope);
        }


        constructor(private $q: angular.IQService, private $rootScope: IInterceptorScope) {
            this.$rootScope.loading = 0;
        }


        public response(response) {
            console.log(response);
            this.$rootScope.loading = 1;
            return response || this.$q.when(response);
        }

        public responseError(rejection) {
            console.log(rejection.status);
            if (rejection.status === 401) {
            }

            return this.$q.reject(rejection);
        }
    }
我得到这个错误:

$q
也未定义。我不知道为什么。有人能帮忙吗

编辑:这是我在
config()
块中添加拦截器的方式,减去其他参数,看起来如下所示:

.config($httpProvider : angular.IHttpProvider) => {
     $httpProvider.interceptors.push(Interceptor.Factory);
}
工厂注册:

.factory('Interceptor', Interceptor)
作为参考,我在这里看到了第一个答案:


$rootScope
在这里有loading属性,但这是纯JavaScript的,因此我不确定它是否与这里的TypeScript方式有关。

Angular调用
response
方法而不设置
this
;要使用TypeScript处理此问题,请使用函数属性捕获此,如:

public response = (response) => {
   console.log(response);
   this.$rootScope.loading = 1;
   return response || this.$q.when(response);
}

responseError也一样。

Angular调用
response
方法,而不设置
this
;要使用TypeScript处理此问题,请使用函数属性捕获此
,如:

public response = (response) => {
   console.log(response);
   this.$rootScope.loading = 1;
   return response || this.$q.when(response);
}

responseError也一样。

您在responseError中没有收到错误,因为您的代码可能没有命中该块。请显示如何注册拦截器?您好,我刚刚重新编辑了我的答案。您在responseError中没有收到错误,因为您的代码可能没有命中该块。请演示如何注册拦截器?您好,我刚刚重新编辑了我的答案。就是这样。非常感谢你!我想问一下,这是否只适用于http拦截器方法,还是在其他情况下会发生这种情况?@Thomas-当您的方法被用作回调时,这很常见,尽管这取决于调用代码。在Angular中有几个地方你会遇到它-例如,指令链接函数。就是这样。非常感谢你!我想问一下,这是否只适用于http拦截器方法,还是在其他情况下会发生这种情况?@Thomas-当您的方法被用作回调时,这很常见,尽管这取决于调用代码。在Angular中有几个地方你会遇到它-例如,指令链接函数。