Loader on angled page

Asked

Viewed 1,896 times

1

  • create a $scope.loading = false, When Voce does any requisicao, Voce arrow true and when you finish arrow false. in html, Voce puts <div ng-if="loading == true"> loading <div>

2 answers

3

One way to achieve this is to know when all pending requests have been answered and to issue a warning when this happens.

To do this, we can use an Angularjs Interceptor that increments a request counter to each request and decreases when this request is answered or suffers some failure. For every action of this we will issue a kind of warning.

angular.module('app').factory('LoadingInterceptor',
  function ($q, $rootScope) {
    var requisicoes = 0;

    return {
        request: function (config) {

            requisicoes++;

            if(requisicoes === 1){
                $rootScope.$broadcast('loading:progredindo')                    
            }

            return config || $q.when(config);
        },

        response: function (response) {
            requisicoes--;

            if(requisicoes === 0){
                $rootScope.$broadcast('loading:terminou')                    
            }

            return response || $q.when(response);
        },

        responseError: function (response) {

            requisicoes--;

            if(requisicoes === 0){
                $rootScope.$broadcast('loading:terminou')                    
            }

            return $q.reject(response);
        }
    };
}).config(function ($httpProvider) {
    $httpProvider.interceptors.push('LoadingInterceptor');
});

Once this is done, simply monitor the events within a directive responsible for the loading screen.

Within the directive:

$rootScope.$on("loading:progredindo", function(){
    //Mostrar tela de loading
});

$rootScope.$on("loading:terminou", function(){
    //Esconder tela de loading
});

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.