Ng-repeat passing a number

Asked

Viewed 75 times

2

I want to mount a select whose options will be generated by an ng-repeat, but for this I want to pass a number, for example if I pass the number 5 in the variable will be mounted 5 options with values 1,2,3,4,5.

<md-select ng-disabled="!novoCadastro.curso" required ng-model="novoCadastro.periodo">
 <md-option ng-value=""></md-option>
 <md-option ng-value="periodo" ng-repeat="periodo in novoCadastro.curso.periodo">{{periodo}}</md-option>
</md-select>  

In my newCadastro.curso.periodo will be where I will declare a number.

1 answer

2


It is not good practice value dynamically generated. I recommend creating a list of dictionaries or tuples and indicating the value and the text.

// dicionario
novoCadastro.curso.periodo = [{1: 'Primeiro'}, {2: 'Segundo'}, ...];
// ou tupla
novoCadastro.curso.periodo = [(1, 'Primeiro'), (2, 'Segundo'), ...];

If you still want to generate value by loop counter, you can use $index;

<md-option ng-value="$index+1" ng-repeat="periodo in novoCadastro.curso.periodo">{{periodo}}</md-option>

The $index starts to count in 0.

  • 1

    Okay, thank you very much, since it is not a good practice I will do as you told me.

  • @Erickzanetti does not forget after mark as accepted answer if solve your problem. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.