(Angular-ui-router) Show loading animation during resolve process

后端 未结 11 808
生来不讨喜
生来不讨喜 2020-12-07 07:12

This is a two part question:

  1. I am using the resolve property inside $stateProvider.state() to grab certain server data before loading the controller. How wo

11条回答
  •  离开以前
    2020-12-07 08:03

    I developed the following solution which works perfectly for me.

    1. Add the following app.run

    app.run(function($rootScope){
    
        $rootScope
            .$on('$stateChangeStart', 
                function(event, toState, toParams, fromState, fromParams){ 
                    $("#ui-view").html("");
                    $(".page-loading").removeClass("hidden");
            });
    
        $rootScope
            .$on('$stateChangeSuccess',
                function(event, toState, toParams, fromState, fromParams){ 
                    $(".page-loading").addClass("hidden");
            });
    
    });
    

    2. Place the loading indicator just above the ui-view. Add id="ui-view" to ui-view div.

    Loading...

    3. add the following to your css

    .hidden {
      display: none !important;
      visibility: hidden !important;
    }
    

    NOTE:

    A. The above code will display loading indicator in two cases 1) when the angular app is loaded first time 2) when the view is changed.

    B. If you don't want the indicator to be shown when the angular app loads first time (before any view is loaded), then add hidden class to the loading div like below

    
    

提交回复
热议问题