[英]Open and close a div after every “x” element
假设我的数组中有8个项目。 在遍历此数组时,我想将它们分成3个项目/页,这将给我3个页面。
逻辑很明确,但我不知道如何使用ng-repeat
实现这一目标。 如果没有直接方法,我也会接受间接方法。
本质上,我想做这样的事情:
$scope.items = ["A", "B", "C", "D", "E", "F", "G", "H"];
$scope.pages = Math.ceil($scope.items.length/3);
<div class="box">
<div class="items" ng-repeat="page in pages">
<!-- put 3 items here and then create a new ".items" div with the next 3 items and so on -->
</div>
</div>
<div class="box">
<div class="items">
<p>A</p>
<p>B</p>
<p>C</p>
</div>
<div class="items">
<p>D</p>
<p>E</p>
<p>F</p>
</div>
<div class="items">
<p>G</p>
<p>H</p>
</div>
</div>
实现目标的想法是通过使用数组的拼接功能将主数组拆分为最小的数组: https : //www.w3schools.com/jsref/jsref_splice.asp
您的拆分功能可能如下所示:
var _split = function(res,arr, n) {
while (arr.length) {
res.push(arr.splice(0, n));
}
}
我创建了一个jsfindle来说明您的要求: http : //jsfiddle.net/ADukg/11761/
尝试以下操作:在角度控制器中:
$scope.items = ["A", "B", "C", "D", "E", "F", "G", "H"];
$scope.pages = Math.ceil($scope.items.length/3);
$scope.itemsArray=[];
while($scope.items.length>0){
$scope.itemsArray.push($scope.items.splice(0,$scope.pages));
}
在html中:
<div ng-repeat="o in itemsArray">
<p ng-repeat="it in o"> {{it}} </p>
</div>`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.