2015-12-21 51 views
0

我遇到了一個問題,我的主頁加載速度比AngularJS中的其他模板快。在所有模板加載之前,延遲加載主頁的最佳方式是什麼?基本上我遇到的問題是,在主頁加載後,用戶在導航欄中單擊不同的路線,並且它不會進入該路線。然後再點擊一下(比如5秒延遲),它就會進入所需的路線(其他所有路線都會在此之後運行)。延遲加載主頁,直到所有模板加載

這裏是$routeProvider方法調用:

ANGULAR

App.config(function ($routeProvider) { 

    $routeProvider 
     .when('/', { 
      templateUrl: home, 
      controller: 'mainController', 
      resolve: mainCont.resolve 
     }) 
     .when('/gym', { 
      templateUrl: gym, 
      controller: 'gymController', 
      resolve: gymCont.resolve 
     }) 
     .when('/instructors', { 
      templateUrl: instructors, 
      controller: 'instructorsController', 
      resolve: instructorCont.resolve 
     }) 
     .when('/classes', { 
      templateUrl: classes, 
      controller: 'classesController', 
      resolve: classCont.resolve 
     }) 
     .when('/media', { 
      templateUrl: media, 
      controller: 'mediaController', 
      resolve: mediaCont.resolve 
     }) 
     .when('/shop', { 
      templateUrl: shop, 
      controller: 'shopController' 
     }) 
     .when('/contact', { 
      templateUrl: contact, 
      controller: 'contactController', 
      resolve: contactCont.resolve 
     }) 
     .when('/admin', { 
      templateUrl: admin, 
      controller: 'adminController' 
     }); 
}); 

我試圖把覆蓋整個屏幕,直到窗口是完全加載的元件,但它不工作對於所有模板:

$(window).on('load', function() { 
     $("#cover").hide(); 
    }); 

回答

0

嘗試使用$routeChangeSuccess事件來設置作用域變量您的加載符號或隱藏目的

app.run(['$rootScope', function($root) { 
    $root.$on('$routeChangeStart', function(e, curr, prev) { 
    if (curr.$$route && curr.$$route.resolve) { 
     // Show a loading message until promises aren't resolved 
     $root.loadingView = true; 
    } 
    }); 
    $root.$on('$routeChangeSuccess', function(e, curr, prev) { 
    // Hide loading message 
    $root.loadingView = false; 
    }); 
}]); 

您可以對一個視圖或所有視圖進行一般操作。並使用ng-show="loadingView"顯示隱藏導航面板。