简体   繁体   English

在每第二个<li>之后使用角度ngRepeat添加<ul> </ ul>

[英]Adding <ul></ul> after every 2nd <li> with angular ngRepeat

I have a ul and I apply ngRepeart on li but I want after every 2nd li it's create new ul Like This. 我有一个ul并且我在li上应用了ngRepeart但是我希望在每个第二个li之后创建新的ul Like This。
is this possible ? 这可能吗 ?

<ul>
  <li>simth</li>
  <li>aryan</li>
</ul>
<ul>
  <li>john</li>
  <li>mark</li>
</ul>
<ul>
  <li>cooper</li>
  <li>lee</li>
</ul>

and this is my angular.js code 这是我的angular.js代码

function myCrtl($scope){
  $scope.nameList= [{
      name:'simth'
    },{
      name:'aryan'
    },{
      name:'john'
    },{
      name:'mark'
    },{
      name:'cooper'
    },{
      name:'lee'
    }]
}

Programming in AngularJS is close to " Data Driven Development ". AngularJS中的编程接近于“ 数据驱动开发 ”。 Your data doesn't reflect the structure of HTML you want to get so it's harder to achieve, but not impossible. 您的数据不会反映您想要获得的HTML结构,因此更难实现,但并非不可能。 My first suggested solution is to change your data's structure. 我的第一个建议的解决方案是更改数据的结构。 If that's not an option, keep on reading. 如果这不是一个选项,继续阅读。

 const myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', $scope => { $scope.nameList = [ [{name: 'simth'}, {name: 'aryan'}], [{name: 'john'}, {name: 'mark'}], [{name: 'cooper'}, {name: 'lee'}] ]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="MyCtrl"> <ul ng-repeat="group in nameList"> <li ng-repeat="person in group">{{person.name}}</li> </ul> </div> </div> 

Alternative 替代

In case you can't change the data structure, you can still do this. 如果您无法更改数据结构,您仍然可以执行此操作。 Implement a little helper function or a filter: 实现一个小帮助函数或过滤器:

 const myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', $scope => { $scope.nameList = [ {name: 'simth'}, {name: 'aryan'}, {name: 'john'}, {name: 'mark'}, {name: 'cooper'}, {name: 'lee'} ]; }); myApp.filter('getGroup', () => (array, number) => { return array.reduce((prev, next, index) => { if (index % number === 0) { // start a new group prev.push([next]); } else { prev[prev.length - 1].push(next); // add to existing group } return prev; }, []); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="MyCtrl"> <ul ng-repeat="group in nameList | getGroup:2"> <li ng-repeat="person in group">{{person.name}}</li> </ul> </div> </div> 

What the filter does is: take the array and divide it into many arrays, each with 2 elements (this is a parameter so you can reuse that later). 过滤器的作用是:取出数组并将其分成许多数组,每个数组包含2个元素(这是一个参数,以便您以后可以重复使用)。

Better to rearrange your data like the others have said, although if you really wanted you could do something like this: 最好像其他人所说的重新排列你的数据,尽管如果你真的想要你可以这样做:

<ul ng-if="$even" ng-repeat="person in nameList">

    <li>{{ person.name }}</li>
    <li>{{ nameList[$index + 1].name }}</li>

</ul>

The ng-if will stop rendering every second item in your list. ng-if将停止呈现列表中的每个第二项。

Although I agree with Miszy, you may be able to achieve it with the following snippet: 虽然我同意Miszy,但您可以使用以下代码段来实现它:

<div ng-repeat="item in nameList">
                <ul ng-if="$index%2 == 0">
                    <li ng-repeat="item in nameList.slice($index, $index+2)">
                        {{ item.name }}
                    </li>
                </ul>
            </div>

well, you can hold the list like so: 好吧,你可以这样保存列表:

function myCrtl($scope){
  $scope.nameList= [{
      name1:'simth'
      name2:'aryan'
    },{
      name1:'john'
      name2:'mark'
    },{
      name1:'cooper'
      name2:'lee'
    }]
}

and then you can do ng-repeat easily 然后你可以轻松地进行重复

<div ng-repeat-start="item in nameList">
  <ul>
    <li>item.name1</li>
    <li>item.name2</li>
  </ul>
</div>

You can split your array into chunks. 您可以将阵列拆分为块。 Please see demo below 请参阅下面的演示

 var app = angular.module('app', []); app.controller('homeCtrl', function($scope) { $scope.nameList = [{ name: 'simth' }, { name: 'aryan' }, { name: 'john' }, { name: 'mark' }, { name: 'cooper' }, { name: 'lee' }] Array.prototype.chunk = function(chunkSize) { var array = this; return [].concat.apply([], array.map(function(elem, i) { return i % chunkSize ? [] : [array.slice(i, i + chunkSize)]; }) ); } $scope.temparray = $scope.nameList.chunk(2) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="homeCtrl"> <ul ng-repeat="gr in temparray"> <li ng-repeat="person in gr">{{person.name}}</li> </ul> </div> </div> 

You can modify the input list and change the format as described by others. 您可以修改输入列表并按其他人的描述更改格式。 I would suggest you to check the generic approach proposed in jsfiddle . 我建议你检查jsfiddle中提出的通用方法。

I wrote a function that will convert your input list to the required format: 我写了一个函数,将您的输入列表转换为所需的格式:

$scope.getSegment = function(noOfRecord){
    noOfRecord = noOfRecord;
    var processedList = [];
    var tempList = [];
    for(var i = 0; i <  $scope.nameList.length; i++){
        tempList.push($scope.nameList[i]);
        if((i+1)%noOfRecord == 0){
            processedList.push(tempList);
            tempList = [];
        }
    }
    return processedList;
};

and iterate it like this: 并迭代它像这样:

<ul ng-repeat="group in getSegment(2)">
    <li ng-repeat="detail in group">{{detail.name}}</li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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