Skip to content

Instantly share code, notes, and snippets.

@doup
Created August 6, 2013 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doup/6166407 to your computer and use it in GitHub Desktop.
Save doup/6166407 to your computer and use it in GitHub Desktop.
Example of AngularJS $http loading global variable
<!-- use $root.loading -->
app.config(['$httpProvider', function ($httpProvider) {
$httpProvider.responseInterceptors.push(['$q', '$rootScope', function($q, $rootScope) {
return function (promise) {
return promise.then(function (response) {
// Success
$rootScope.loadingCount--;
$rootScope.loading = $rootScope.loadingCount !== 0;
return response;
}, function (response) {
// Error
$rootScope.loadingCount--;
$rootScope.loading = $rootScope.loadingCount !== 0;
return $q.reject(response);
});
};
}]);
}]);
app.run(['$http', '$rootScope', function ($http, $rootScope) {
$rootScope.loadingCount = 0;
$rootScope.loading = false;
$http.defaults.transformRequest.push(function (data) {
$rootScope.loadingCount++;
$rootScope.loading = $rootScope.loadingCount !== 0;
return data;
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment