How to display wait message in AngularJS while data loading?

后端 未结 5 728
余生分开走
余生分开走 2021-02-04 03:22

I\'m new in AngularJS and trying to find the way how to display wait message while data loading? I mean data starts loading, display message and remove it when data loading is d

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 03:42

    Depends from where you're loading the data. One solution I used was to create a LoadingService

    app.factory('LoadingService', function($rootScope) {
        return {
            loading : function(message) {
                 $rootScope.loadingMessage = message;
            },
            loaded : function() {
                 $rootScope.loadingMessage = null;
            }
        }
    }).controller('FooController', function($scope,$http,LoadingService) {
    
       $scope.loadSomeData = function() {
           LoadingService.loading('Data is loading');
    
           $http.get('/data').finally(function() {
                LoadingService.loaded();
           });
       };
    });
    

    Since I had only one place where the message was being displayed I could use RootScope to handle this. If you want to have a loading message multiple times you could write a directive also to handle this like Codezilla posted

提交回复
热议问题