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>
I think this jsFiddle from this thread might be what you're looking for.
angular gives a very sweet function called slice.. using this you can achieve what you are looking for. e.g. ng-repeat="ab in abc.slice(startIndex,endIndex)"
this demo :http://jsfiddle.net/sahilosheal/LurcV/39/ will help you out and tell you how to use this "making life easy" function. :)
html:
CSS:
ng-JS:
There are many ways to do this. I was really bothered at having the logic in my controller so I created a simple directive to solve the problem of repeating an element n-times.
Installation:
The directive can be installed using
bower install angular-repeat-n
Example:
produces:
1234
It also works using a scope variable:
Source:
Github
For users using CoffeeScript, you can use a range comprehension:
Directive
or Controller
Template
I am creating a reusable directive where the max number will come from another ng-repeat. So, this is an edit over the best voted answer.
Just change the code at controller to this -
This will return a new array with specified number of iterations
I needed a more dynamic solution to this - where I could increment the repeat.
HTML
Duplicator Control
JS