javascript - Adding <ul></ul> after every 2nd <li> with angular ngRepeat -
i have ul , apply ngrepeart on li want after every 2nd li it's create new ul 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 angular.js code
function mycrtl($scope){ $scope.namelist= [{ name:'simth' },{ name:'aryan' },{ name:'john' },{ name:'mark' },{ name:'cooper' },{ name:'lee' }] }
you can split array 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 % chunksize ? [] : [array.slice(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>
Comments
Post a Comment