我给了Gloopy一个解决方案,但是,他引用的
other post在DOM和拦截器定义的函数中进行DOM操作.相反,我将启动微调器的逻辑移到了inter the的顶部,我在$rootScope中使用一个变量来控制微调器的隐藏/显示.它似乎工作得很好,我相信是更加可测试.
原文链接:https://www.f2er.com/angularjs/140733.html<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