marcosmfjunior
marcosmfjunior

Reputation: 13

How can I set a variable into ng-repeat from controller?

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

Answers (2)

Cosmin Ababei
Cosmin Ababei

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

tymeJV
tymeJV

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

Related Questions