Skip to content

Instantly share code, notes, and snippets.

@ianjosephwilson
Created September 27, 2013 19:57
Show Gist options
  • Save ianjosephwilson/6734333 to your computer and use it in GitHub Desktop.
Save ianjosephwilson/6734333 to your computer and use it in GitHub Desktop.
loading notification
app.config(function($routeProvider, $locationProvider, $httpProvider) {
// ...
$httpProvider.responseInterceptors.push(function ($q, $rootScope) {
return function (promise) {
$rootScope.$broadcast('startLoading');
return promise.then(function (response) {
$rootScope.$broadcast('stopLoading');
return response;
}, function (response) {
$rootScope.$broadcast('stopLoading');
return $q.reject(response);
});
};
});
});
app.directive('appLoadingNotification', function () {
return {
template: '<div ng-show="isLoading()" class="alert alert-info">Loading ...</div>',
replace: true,
restrict: 'A',
link: function (scope) {
var loading = 0;
scope.$on('startLoading', function () {
loading += 1;
});
scope.$on('stopLoading', function () {
loading -= 1;
});
scope.isLoading = function () {
return loading > 0;
};
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment