Show progress circular during $http post?

后端 未结 7 1286
名媛妹妹
名媛妹妹 2021-02-08 16:05

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

7条回答
  •  悲&欢浪女
    2021-02-08 16:28

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

提交回复
热议问题