我正在努力做到这一点,以便我可以将“requestCounter”添加到任何控制器并获得一个随着请求数量不断更新的值。拦截器代码正在工作,但是注入 requestCounter 提供的值始终是{count: 0}
. 我不明白什么!
angular.module('theApp')
.provider('requestCounter', function ($httpProvider) {
this.$get = function () {
var activeRequests = 0;
var obj = {count: activeRequests};
$httpProvider.defaults.transformRequest.push(function(data) {
activeRequests++;
return data;
});
$httpProvider.defaults.transformResponse.push(function(data) {
activeRequests--;
return data;
});
return obj;
};
});
控制器
angular.module('theApp')
.controller('PurchaseCtrl', function ($scope, requestCounter) {
$scope.requests = requestCounter;
});
标记
<h1>There are {{requests.count}} requests loading</h1>