How to display wait message in AngularJS while data loading?

后端 未结 5 721
余生分开走
余生分开走 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 04:09

    I've answered this question in this StackOverflow article, but here's a recap of what I did.

    If you style your code correctly, and make sure all calls to a web service pass through one particular factory function, then you can make that factory function handle showing and hiding your "Please Wait" popup.

    Here's the factory function which I use to call all of my GET web services:

    myApp.factory('httpGetFactory', function ($http, $q) {
        return function (scope, URL) {
            //  This Factory method calls a GET web service, and displays a modal error message if something goes wrong.
            scope.$broadcast('app-start-loading');          //  Show the "Please wait" popup
    
            return $http({
                url: URL,
                method: "GET",
                headers: { 'Content-Type': undefined }
            }).then(function (response) {
                scope.$broadcast('app-finish-loading');     //  Hide the "Please wait" popup
                if (typeof response.data === 'object') {
                    return response.data;
                } else {
                    // invalid response
                    return $q.reject(response.data);
                }
            }, function (errorResponse) {
                scope.$broadcast('app-finish-loading');     //  Hide the "Please wait" popup
    
                //  The WCF Web Service returned an error.  
                //  Let's display the HTTP Status Code, and any statusText which it returned.
                var HTTPErrorNumber = (errorResponse.status == 500) ? "" : "HTTP status code: " + errorResponse.status + "\r\n";
                var HTTPErrorStatusText = errorResponse.statusText;
    
                var message = HTTPErrorNumber + HTTPErrorStatusText;
    
                BootstrapDialog.show({
                    title: 'Error',
                    message: message,
                    buttons: [{
                        label: 'OK',
                        action: function (dialog) {
                            dialog.close();
                        },
                        draggable: true
                    }]
                });
    
                return $q.reject(errorResponse.data);
            });
        };
    });
    

    This would get called like this:

    myApp.webServicesURL = "http://localhost:15021/Service1.svc";
    
    var dsLoadAllEmployees = function (scope)
    {
         //  Load all survey records, from our web server
         $scope.LoadingMessage = "Loading Employees data...";
    
         var URL = myApp.webServicesURL + "/loadAllEmployees";
         return httpGetFactory(scope, URL);
    }
    

    Here's the "Please wait" control which I use on each page..

    
    

    ... and its code looks like this...

    myApp.directive('pleaseWait',  
        function ($parse) {
            return {
                restrict: 'E',
                replace: true,
                scope: {
                    message: '@message'
                },
                link: function (scope, element, attrs) {
                    scope.$on('app-start-loading', function () {
                        element.fadeIn(); 
                    });
                    scope.$on('app-finish-loading', function(){
                        element.animate({
                            top: "+=15px",
                            opacity: "0"
                        }, 500);
                    });
                },
                template: '
    {{ message }}
    ' } });

    Using this structure, any of my Angular controllers can load data from a web service in just a few lines, and leave the factory to look after showing/hiding the "Please wait" message and to display any errors which occur:

       $scope.LoadAllSurveys = function () {
            DataService.dsLoadAllSurveys($scope).then(function (response) {
                //  Success
                $scope.listOfSurveys = response.GetAllSurveysResult;
            });
       }
    

    Nice, hey ?

提交回复
热议问题