Reputation: 13
I have this variable in my Controller:
$scope.numeroDescontos = 5;
I have one button that iterate the variable, and I want to build one 'ng-repeat' using that.
What's the best way to do that?
Upvotes: 0
Views: 416
Reputation: 7072
Why not use Angular's built-in limitTo filter? This filter creates a new array or string containing only a specified number of elements.
You can use it like this:
<div ng-repeat="item in items | limitTo: numberOfElements">
Upvotes: 0
Reputation: 104795
Well, ngRepeat
works over an Array - so you can have a function that'll take that number and return an array of that length - then repeat over that:
$scope.makeArray = function(num) {
var arr = [];
for (var i = 0; i < num; i++) {
arr.push(i);
}
return arr;
}
Then the HTML:
<div ng-repeat="num in makeArray(numeroDescontos)">Row {{num + 1}}</div>
Upvotes: 2