繁体   English   中英

在每个“ x”元素之后打开和关闭div

[英]Open and close a div after every “x” element

假设我的数组中有8个项目。 在遍历此数组时,我想将它们分成3个项目/页,这将给我3个页面。

逻辑很明确,但我不知道如何使用ng-repeat实现这一目标。 如果没有直接方法,我也会接受间接方法。

本质上,我想做这样的事情:

JS

$scope.items = ["A", "B", "C", "D", "E", "F", "G", "H"];
$scope.pages = Math.ceil($scope.items.length/3);

的HTML

<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.

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