Hi,
I want to create a service that tracks of the number of http requests and responses using the '$httpProvider.interceptors'. Please see the following code. I had to wrap it within a closure to hide "activeHttpRequestCount" from the global scope and expose it only through the httpInterceptorService.
This code works exactly what I need, but I would like to know if there's a more elegant solution to sharing variables between a service and the intercepting function that i'm passing into $httpProvider.interceptors.
var myApp= angular.module('myApp', []);
(function () {
var activeHttpRequestCount = 0;
myApp.service('httpInterceptorService', function () {
return {
getActiveRequestCount: function () {
return activeHttpRequestCount;
}
};
}).config(['$httpProvider',
function ($httpProvider) {
$httpProvider.interceptors.push(function ($q) {
return {
request: function (config) {
activeHttpRequestCount++;
return config;
},
response: function (response) {
activeHttpRequestCount--;
return response;
}
};
});
}
]);
})();
Thanks,
-Trinh