Definir a variável rootScope no httpIntercept
Eu gostaria de ser capaz de configurar um interceptor http AngularJS que irá definir$rootScope.loading
paratrue
oufalse
, dependendo se uma solicitação AJAX está em andamento no momento.
Até agora eu coloquei o seguinte em conjunto:
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);
});
};
});
Mas eu sou incapaz de injetar$rootScope
no bloco de configuração, então eu não tenho como configurar$rootScope.loading
variável quando uma solicitação HTTP começa.
Estou faltando alguma coisa aqui? Como devo fazer isso?