how can i add one common loader or progress bar to entire project

邮差的信 提交于 2019-12-08 03:36:50

问题


I'm doing my project in angularjs how can i add one common loader or progress bar to entire project with css rather than calling show/hide block functions for every controller's div class.


回答1:


You have to write an interceptor to do that. To learn about interceptor refer: https://docs.angularjs.org/api/ng/service/$http

But someone has already done all the works, so why re-invent the wheel! http://chieffancypants.github.io/angular-loading-bar/




回答2:


Until you get the response you can put a waiting dialog.. after getting the response you can hide the dialog.. Here is a link of simple dialog using jquery waitingDialog

And this dialog can be best used with angular and you can alter it according to your requirement so good luck..

Do it something like this

angular.module('app').factory('httpInterceptor', ['$q', '$rootScope',
    function ($q, $rootScope) {
        var loadingCount = 0;

        return {
            request: function (config) {
                if(++loadingCount === 1) $rootScope.$broadcast('loading:progress');
                return config || $q.when(config);
            },

            response: function (response) {
                if(--loadingCount === 0) $rootScope.$broadcast('loading:finish');
                return response || $q.when(response);
            },

            responseError: function (response) {
                if(--loadingCount === 0) $rootScope.$broadcast('loading:finish');
                return $q.reject(response);
            }
        };
    }
]).config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('httpInterceptor');
}]);

Then use event bound to $rootScope anywhere (preferable to use in directive or run block of app.js):

$rootScope.$on('loading:progress', function (){
    // show loading gif
});

$rootScope.$on('loading:finish', function (){
    // hide loading gif
});

You can read more about it here codingsmackdown




回答3:


You can use angular-loading-bar http://chieffancypants.github.io/angular-loading-bar/#

This is the best progress bar working in background.

Here is the documentation http://angular-js.in/loading-bar/

Also the demo is available there.

To change color of loading bar use css

#loading-bar .bar {
    background-color: #2CA01C;
}


来源:https://stackoverflow.com/questions/38177329/how-can-i-add-one-common-loader-or-progress-bar-to-entire-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!