Setzen Sie die rootScope-Variable auf httpIntercept

Ich möchte in der Lage sein, einen AngularJS-http-Interceptor einzurichten, der das setzt$rootScope.loading zutrue oderfalseDies hängt davon ab, ob gerade eine AJAX-Anforderung ausgeführt wird.

Bisher habe ich Folgendes zusammengestellt:

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);
        });
    };
});

Aber ich kann nicht spritzen$rootScope in den config block, also habe ich keine möglichkeit das einzustellen$rootScope.loading Variable, wenn eine HTTP-Anforderung beginnt.

Vermisse ich hier etwas? Wie soll ich das machen?

Antworten auf die Frage(3)

Ihre Antwort auf die Frage