Establecer la variable rootScope en httpIntercept
Me gustaría poder configurar un interceptor de http AngularJS que establecerá$rootScope.loading
atrue
ofalse
, dependiendo de si una solicitud AJAX está actualmente en curso.
Hasta ahora he puesto los siguientes juntos:
angular.module('myApp')
.config(function ($httpProvider) {
$httpProvider.responseInterceptors.push('loadingInterceptor');
var loadingFunction = function (data, headersGetter) {
$rootScope.loading = true
return data;
};
$httpProvider.defaults.transformRequest.push(loadingFunction);
})
.factory('loadingInterceptor', function ($q, $window, $rootScope) {
return function (promise) {
return promise.then(function (response) {
$rootScope.loading = false;
return response;
}, function (response) {
$rootScope.loading = false;
return $q.reject(response);
});
};
});
Pero no puedo inyectar$rootScope
en el bloque de configuración, así que no tengo manera de configurar el$rootScope.loading
variable cuando comienza una solicitud HTTP.
¿Me estoy perdiendo de algo? ¿Cómo debería hacer esto?