0

I am using angular 1.6.4 I want to display ages in select box so get values from controller, I use below code, but first field will come empty? please help me how to solve it

  <select ng-model="agefrom" name="agefrom" id="agefrom" class="select" style="width: 45%">
    <option ng-repeat="age in ages" ng-value="age">{{age}}</option>
  </select>
1
  • 1
    Read the documentation about ngValue and you should understand where you went wrong. Next, read the docs about ngOptions and you'll know how to do it right Commented May 17, 2017 at 10:01

2 Answers 2

1

the first option comes empty because you did not select a ngModel. add value to ngModel and also use ng-options instead ng-repeat

angular.module("app",[])
.controller("ctrl",function($scope){
$scope.ages = [2,3,4];

$scope.agefrom = $scope.ages[0]

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <select ng-model="agefrom" name="agefrom" id="agefrom" class="select" style="width: 45%" ng-options="age as age for age in ages"> 
 </select>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

You can simply use ng-init like this

<select ng-init="agefrom = ages[0]" 
    ng-model="agefrom" 
    name="agefrom" id="agefrom" class="select" 
    style="width: 45%">
    <option ng-repeat="age in ages" ng-value="age">{{age}}</option>
</select>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.