435

Is there a way to ng-repeat a defined number of times instead of always having to iterate over an array?

For example, below I want the list item to show up 5 times assuming $scope.number equal to 5 in addition incrementing the number so each list item increments like 1, 2, 3, 4, 5

Desired result:

<ul>
   <li><span>1</span></li>
   <li><span>2</span></li>
   <li><span>3</span></li>
   <li><span>4</span></li>
   <li><span>5</span></li>
</ul>
Dan
  • 59,490
  • 13
  • 101
  • 110
Malcr001
  • 8,179
  • 9
  • 44
  • 57

27 Answers27

590

Update (9/25/2018)

Newer versions of AngularJS (>= 1.3.0) allow you to do this with only a variable (no function needed):

<li ng-repeat="x in [].constructor(number) track by $index">
    <span>{{ $index+1 }}</span>
</li>
$scope.number = 5;

This was not possible at the time the question was first asked. Credit to @Nikhil Nambiar from his answer below for this update


Original (5/29/2013)

At the moment, ng-repeat only accepts a collection as a parameter, but you could do this:

<li ng-repeat="i in getNumber(number)">
    <span>{{ $index+1 }}</span>
</li>

And somewhere in your controller:

$scope.number = 5;
$scope.getNumber = function(num) {
    return new Array(num);   
}

This would allow you to change $scope.number to any number as you please and still maintain the binding you're looking for.

EDIT (1/6/2014) -- Newer versions of AngularJS (>= 1.1.5) require track by $index:

<li ng-repeat="i in getNumber(number) track by $index">
    <span>{{ $index+1 }}</span>
</li>

Here is a fiddle with a couple of lists using the same getNumber function.

Community
  • 1
  • 1
Dan
  • 59,490
  • 13
  • 101
  • 110
  • 3
    $scope.getNumber = function(num) { var x=new Array(); for(var i=0;i – Manavendher Sep 25 '15 at 13:13
  • @Manavendher Incorrect. We do not need to populate the array, we only need it to be the length of `$scope.number`. Angular's digest cycle will automatically recall this function and resize the array any time `$scope.number` changes. – Dan Sep 25 '15 at 16:23
  • 1
    @sh0ber. When I need to change the list items based on the value selected in number dropdown. I tried your function first, when it is not working, I changed it to above to update the no. of list items when different number is selected in the dropdown. Can you please check my scenario using your function. – Manavendher Sep 26 '15 at 16:44
  • @Manavendher I tried with angular 1.2.x and 1.5.x with same results. Links are 1. http://stackoverflow.com/questions/40202263/re-render-ngrepeat-based-on-user-typed-value-input-field 2. https://plnkr.co/edit/zcPr7oUEBBWvCSVkDvml – HalfWebDev Oct 23 '16 at 11:39
144

you can do this:

<div ng-repeat="i in [1, 2, 3, 4]">
  ...
</div>
akonsu
  • 28,824
  • 33
  • 119
  • 194
  • @AdityaMP, use this for ranges in javascript http://stackoverflow.com/a/31989462/3160597 – azerafati Jul 16 '16 at 08:00
  • 1
    Consider calling the array constructor for a dynamic-length array, as stated in [this answer](https://stackoverflow.com/a/44594836/3045563). – maxathousand Oct 27 '17 at 14:37
  • 1
    what if i have to repeat 120 times. Should i go with [1,2,3... 120]? – Munkhdelger Tumenbayar Feb 13 '20 at 03:20