Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - how to use ng-repeat in different containers

I am trying to use ng-repeat to display the array of numbers. How to achieve this if I want it to be in different containers, for example in different ul? Also, each ul can only contain at most 2 li.

$scope.numbers = [1,2,3,4,5]

<div ng-controller="myController">
    <ul>
        <li>1</li>
        <li>2</li>
    </ul>
    <ul>
        <li>3</li>
        <li>4</li>
    </ul>
    <ul>
        <li>5</li>
    </ul>
</div>
like image 788
teggy Avatar asked Feb 17 '23 20:02

teggy


1 Answers

You should split that numbers array into chunks, then use a nested ng-repeat.

JavaScript:

var i, l = $scope.numbers.length;

$scope.chunks = [];

for ( i = 0; i < l; i += 2) {
    $scope.chunks.push( $scope.numbers.slice(i, i + 2) );
}

HTML:

<div ng-controller="myController">
    <ul ng-repeat="chunk in chunks">
        <li ng-repeat="number in chunk">{{ number }}</li>
    </ul>
</div>

See it here in action.

like image 132
Joseph Silber Avatar answered Feb 19 '23 09:02

Joseph Silber