我正在尝试这样做,以便我可以将“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>
答案 0 :(得分:0)
在开始时,您要为您的obj分配activeRequests = 0的原始值,因此它们不会相互链接。您可以尝试:
var activeRequests = {number: 0};
var obj = {count: activeRequests};
然后你可以在obj.count.number
中获得你的计算数字