What is the best way to use Angular Material\'s Progress circular component with a $http
request?
I currently have the code like this below:
Progre
You should hook up to $httpProvider
's events:
angular.module('common')
.config(['$httpProvider', function($httpProvider){
$httpProvider.interceptors.push(['$rootScope', '$q', '$timeout',
function($rootScope, $q, $timeout) {
return {
request: function(config) {
$rootScope.posting = new Date().getTime();
$rootScope.$broadcast('$postingStart', config.url);
return $q.resolve(config);
},
response: function(response) {
$rootScope.posting = false;
$rootScope.$broadcast('$postingEnd', response.config.url);
return $q.resolve(response);
},
responseError: function(response) {
$rootScope.posting = false;
$rootScope.$broadcast('$postingEnd', response.config.url);
return $q.reject(response);
}
};
}]);
}])
.run(['$mdDialog', '$rootScope', function($mdDialog, $rootScope){
var showing = false;
function showWait() {
if(showing) return;
$mdDialog.show({
controller: 'waitCtrl',
template: '' +
'' +
' ' +
'' +
' ',
parent: angular.element(document.body),
clickOutsideToClose: false,
fullscreen: false
})
.then(function(answer) {
showing = false;
});
}
$rootScope.$on('$postingStart', function(event, url) {
if (!$rootScope.postingStartTimer) {
$rootScope.postingStartTimer = $timeout(function() {
showWait()
}, 250);
}
});
$rootScope.$on('$postingEnd', function(event, url) {
if ($rootScope.postingStartTimer && posts.length === 0) {
$timeout.cancel($rootScope.postingStartTimer);
$rootScope.postingStartTimer = false;
if(!showing) return;
$mdDialog.cancel();
}
});
}])