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>
If n is not too high, another option could be to use split('') with a string of n characters :
you can do this:
You can use the
ng-if
directive withng-repeat
So, if num is the number of times you need the element repeated:
since iterating over a string it will render an item for each char:
we can use this ugly but no-code workaround using the
number|n decimal places
native filter.this way we'll have
mynumber
elements with no extra code. Say'0.000'
.We use
mynumber - 2
to compensate0.
It won't work for numbers below 3, but might be useful in some cases.
I wanted to keep my html very minimal, so defined a small filter that creates the array [0,1,2,...] as others have done:
After that, on the view is possible to use like this:
You can use this example.
Inside controller:
Example for select element at the HTML code side: