加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angularjs后接收钩或类似?

发布时间:2020-12-17 09:17:46 所属栏目:安全 来源:网络整理
导读:有没有办法在从服务器返回响应后每次都调用一个函数,而不是在回调之后显式调用它? 主要目的是我有一个通用的错误处理程序服务,我在每个请求的回调函数中调用,我想在某处指定它,它将被自动调用. 我给了Gloopy一个解决方案,但是,他引用的 other post在DOM和拦
有没有办法在从服务器返回响应后每次都调用一个函数,而不是在回调之后显式调用它?

主要目的是我有一个通用的错误处理程序服务,我在每个请求的回调函数中调用,我想在某处指定它,它将被自动调用.

我给了Gloopy一个解决方案,但是,他引用的 other post在DOM和拦截器定义的函数中进行DOM操作.相反,我将启动微调器的逻辑移到了inter the的顶部,我在$rootScope中使用一个变量来控制微调器的隐藏/显示.它似乎工作得很好,我相信是更加可测试.
<img ng-show="polling" src="images/ajax-loader.gif">
angular.module('myApp.services',['ngResource']).
.config(function ($httpProvider) {
    $httpProvider.responseInterceptors.push('myHttpInterceptor');
    var spinnerFunction = function (data,headersGetter) {
        return data;
    };
    $httpProvider.defaults.transformRequest.push(spinnerFunction);
})
//register the interceptor as a service,intercepts ALL angular ajax http calls
.factory('myHttpInterceptor',function ($q,$window,$rootScope) {
    return function (promise) {
        $rootScope.polling = true;
        return promise.then(function (response) {
            $rootScope.polling = false;
            return response;
        },function (response) {
            $rootScope.polling = false;
            $rootScope.network_error = true;
            return $q.reject(response);
        });
    };
})
// other code left out

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读