2015-10-05 132 views
0

我有一個顯示預加載器的div。只有在我的所有數據在我的重複中完成加載後,我纔想隱藏該div。ng-repeat的AngularJS簡單預加載器?

到目前爲止,這是我可以拼湊在一起,但件還沒有工作。

是否有另一種簡單的方法來做到這一點?

Plunkerhttp://plnkr.co/edit/ilgOZzIy2axSi5Iy85C7?p=preview

HTML:

<div ng-controller="locationAccordionCtrl"> 
    <div ng-repeat="location in locations" on-finish-render="ngRepeatFinished"> 

     {{location.siteName}} 

     <ul> 
      <li ng-repeat="listOfLocations in location.locationsList track by $index"> 
       {{listOfLocations}} 
      </li> 
     </ul> 

    </div> 
</div> 

控制器:

App.controller('locationAccordionCtrl', function ($scope) { 

    $scope.locations = [ 
    { 
     "siteName":"First Location", 
     "locationsList":['First Location 1', 'First Location 2', 'First Location 3'] 
    }, 
    { 
     "siteName":"Second Location", 
     "locationsList":['Second Location 1', 'Second Location 2', 'Second Location 3'] 
    }, 
    { 
     "siteName":"Third Location", 
     "locationsList":['Third Location 1', 'Third Location 2', 'Third Location 3'] 
    } 
    ]; 

// Hide preloader after repeater is finished loading data 
App.directive('onFinishRender', function ($timeout) { 
    return { 
     restrict: 'A', 
     link: function (scope, element, attr) { 
      if (scope.$last === true) { 
       $timeout(function() { 
        scope.$emit('ngRepeatFinished'); 
       }); 
      } 
     } 
    } 
}); 

// hide preloader 
$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) { 
    $scope.hidePreLoader = true; 
}); 

}); 

回答

1

這裏是你如何能做到這一點

var App = angular.module('App.directives', []); 
    App.directive('routeLoadingIndicator', function($rootScope){ 
     return { 
     restrict:'E', 
     template:"<h1 ng-if='isRouteLoading' 
     style=' 
     position: fixed; 
     width: 100%; 
     height: 100%; 
     background: rgb(255, 255, 255) none repeat scroll 0% 0%; 
     margin: 0px; padding: 0px; left: 0px; 
     top: 0px; right: 0px; bottom: 0px; 
     z-index: 2147483647; 
     ' >Loading...</h1>", 
     link:function(scope, elem, attrs){ 
     scope.isRouteLoading = false; 

     $rootScope.$on('$routeChangeStart', function(){ 
      scope.isRouteLoading = true; 
     }); 

     $rootScope.$on('$routeChangeSuccess', function(){ 
      scope.isRouteLoading = false; 
     }); 
     } 
    }; 
    }); 
+0

你可以提供一個plunker顯示這方面的工作? http://plnkr.co/edit/ilgOZzIy2axSi5Iy85C7?p=preview – simple

+0

http://plnkr.co/edit/voIPmpxgiuXmhZlvaZJc?p=preview 根據您的需要修改模板 – Punit