CrazzyMarc CrazzyMarc - 10 months ago 36
AngularJS Question

How to change the number of select box options

I want to change the number of elements to the number given by a select box.
My code:


<select class="form-control-static" ng-model="fieldSelect" ng-change="showSelectValue(fieldSelect)">
<option ng-repeat="i in getNumber(number) track by $index" value="{{$index+1}}">{{$index+1}}</option>


<div ng-repeat="t in getTimes(1) track by $index")>


$scope.showSelectValue = function(fieldSelect) {
contentFields = fieldSelect;

n = contentFields;
return new Array(n);

Answer Source

Try this working example.

var myApp = angular.module('myApp',[]);

myApp.controller('GreetingController', ['$scope', function($scope) {
   // Parse the value to int
   return new Array(parseInt(n));
<script src=""></script>
<div ng-app="myApp" ng-controller="GreetingController">
<select class="form-control-static" ng-model="fieldSelect" ng-init="fieldSelect=1">
    <option ng-repeat="i in [1,2,3,4,5] track by $index" value="{{$index+1}}">{{$index+1}}</option>
<div ng-repeat="t in getTimes(fieldSelect) track by $index")>
    Hello! {{$index}}