简体   繁体   中英

Angular CSS ngRepeat with separator between li

I want to make a simple custom pagination, but I do not know how to do separators between two iterations of ngRepeat. Currently the separators are at the end and not between.

 function Controller($scope) { $scope.currentPage = 1; $scope.totalPages = 2; $scope.itemPerPage = 12; $scope.getPages = function () { return new Array($scope.totalPages); }; $scope.pageChange = function (page) { if(isNaN(page)) { if((page == "-" && 1 < $scope.currentPage) || (page == "+" && $scope.currentPage < $scope.totalPages)) { $scope.currentPage = eval($scope.currentPage + page + 1); } } else { $scope.currentPage = page; } }; } 
 ul { list-style: none; display: inline-block; vertical-align: middle; margin-bottom: 0; } li { display: inline-block; vertical-align: middle; margin: 0 -1px; } a.active { background: #31a445; color: #fff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="" ng-controller="Controller"> <ul> <li ng-repeat="page in getPages() track by $index"> <a href="" ng-click="pageChange($index + 1)" ng-class="{active: currentPage == $index + 1}">{{$index + 1}}</a> </li> <li ng-hide="$last" class="paging-spacer"><span>-</span></li> </ul> </div> 

You should use ng-repeat-start and ng-repeat-end to create block that is repeated

<li ng-repeat-start="page in getPages() track by $index">
   <a href="" ng-click="pageChange($index + 1)" ng-class="{active: currentPage == $index + 1}">{{$index + 1}}</a>
</li>
<li ng-hide="$last" ng-repeat-end class="paging-spacer"><span>-</span></li>

Your separator is outside the ng-repeat - use ng-repeat-start and ng-repeat-end as below:

 function Controller($scope) { $scope.currentPage = 1; $scope.totalPages = 2; $scope.itemPerPage = 12; $scope.getPages = function() { return new Array($scope.totalPages); }; $scope.pageChange = function(page) { if (isNaN(page)) { if ((page == "-" && 1 < $scope.currentPage) || (page == "+" && $scope.currentPage < $scope.totalPages)) { $scope.currentPage = eval($scope.currentPage + page + 1); getProducts(); } } else { $scope.currentPage = page; getProducts(); } }; } 
 ul { list-style: none; display: inline-block; vertical-align: middle; margin-bottom: 0; } li { display: inline-block; vertical-align: middle; margin: 0 -1px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="" ng-controller="Controller"> <ul> <li ng-repeat-start="page in getPages() track by $index"> <a href="" ng-click="pageChange($index + 1)" ng-class="{active: currentPage == $index + 1}">{{$index + 1}}</a> </li> <li ng-hide="$last" class="paging-spacer"><span>-</span> </li> <li ng-repeat-end></li> </ul> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM