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>
This is only a slight variation on the accepted answer, but you don't really need to create a new function. Only to import 'Array' in the scope:
See this fiddle for a live example.
This is really UGLY, but it works without a controller for either an integer or variable:
integer:
variable:
simple way:
in the component/controller and then:
This code is from my typescript project but could be rearranged to pure javascript
A simpler approach would be (for an example of 5 times):
I encountered the same problem and this is what I came out with:
... and the html:
LIVE EXAMPLE
I used underscore's
times
function as we where already using it on the project, but you can easily replace that with native code.Here is an example of how you could do this. Note that I was inspired by a comment in the ng-repeat docs: http://jsfiddle.net/digitalzebra/wnWY6/
Note the ng-repeat directive:
Here is the controller: