30
<select ng-model="team.captain" ng-options="player.name for player in team.players"></select>

This correctly creates a select list to choose the team captain. However, by default a blank option is selected. How can we preselect the first player from the list instead?

<select ng-model="team.captain" ng-options="player.name for player in team.players" class="ng-pristine ng-valid">
  <option value="0">John</option>
  <option value="1">Bobby</option>
</select>

I tried adding ng-init="team.captain='0'" but that didn't help.

Update Apparently this happens because

a value referenced by ng-model doesn't exist in a set of options passed to ng-options.

Source: Why does AngularJS include an empty option in select?

However, the question still remains why using ng-init doesn't work?

<select ng-init="team.captain='0'" ng-model="team.captain" ng-options="player.name for player in team.players"></select>
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
randomguy
  • 12,042
  • 16
  • 71
  • 101

6 Answers6

34

Here's what worked:

<select ng-init="team.captain=team.players[0]" 
        ng-model="team.captain" 
        ng-options="player.name for player in team.players"></select>

And what didn't work:

ng-init="team.captain='0'"
ng-init="team.captain='John'"

My guess is that Angular goes beyond simple comparison of values or labels. It probably compares object references.

randomguy
  • 12,042
  • 16
  • 71
  • 101
  • 3
    I should have mentioned in my answer that the "label" value needs to be reference to the same object in the array. In other words, this won't work either: ng-init="{name: 'John'}" – Mark Rajcok Jan 12 '13 at 21:04
  • In case anyone here is trying to achieve the same thing with angular material md-select then check out this answer http://stackoverflow.com/a/38639823/3836923 – Japheth Ongeri - inkalimeva Jul 29 '16 at 05:42
15

Here's an alternate method for initializing a drop down menu using AngularJS.

(working example on JS Fiddle: http://jsfiddle.net/galeroy/100ho18L/1/)

<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>

<body ng-app="" ng-controller="myController">

    <select 
        ng-model="carSelection"
        ng-options = "x.make for x in cars">
    </select>

    <script>

        function myController($scope) {

            $scope.cars = [
                {"make": "Nissan", "model": "Sentra"},
                {"make": "Honda", "model": "Prelude"},
                {"make": "Toyota", "model": "Prius"}
            ]

            $scope.carSelection = $scope.cars[1]; // this line initializes the drop down menu
        }

    </script>

</body>

Gerald LeRoy
  • 1,227
  • 2
  • 11
  • 17
  • hey you seem like familiar with initializing combo boxes , just question if it doesn't bother you , this one if you have one select box ope.carSelection = $scope.cars[1]; what if i want to initialize in a dynamic number of combo boxes and i want to initilize them all the same value what would i do here's my question http://stackoverflow.com/questions/37351051/ng-option-intial-value-select and thank you – Kamel Mili May 23 '16 at 08:58
  • key that it had to be a reference to the same object, I tried using a different object with a corresponding id. +1 – chiliNUT Mar 06 '17 at 23:49
5

As @camus already mentioned in a comment, you need to set the model to a valid "label" value (or reference), not an index value. This is a bit odd since you can see an index value being used in the HTML.

Angular sets the value attributes in the HTML as follows:

  • when using array as datasource, it will be the index of array element in each iteration;
  • when using object as datasource, it will be the property name in each iteration.

When an item is selected, Angular looks up the correct entry in the array/object based on the index or property name.

Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
4

What about ng-selected, as seen in this jsfiddle:

<select ng-model="val">
  <option ng-repeat="opt in options" ng-selected="$first">
    {{ opt }}
  </option>
</select>
Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
1

I achieved this using ng-model and ng-options.Basically your model and ng-options should be in sync.

When my model (projIndustry in this case) was initialized to some number then I had to use ind.ID in ng-options.(ID comparison).
When my model was initialized to object ,then I had to use ind(object) in ng-options.(Object comparison)

<select data-ng-options="ind.ID as ind.Display_Text for ind in arrIndustries" 
 data-ng-model="projIndustry" ng-change="onIndChange()" />
Amardeep Kohli
  • 503
  • 3
  • 7
0

In case you are using angular 2+, here is what worked. `

<mat-select matNativeControl placeholder="Select Item" [(ngModel)]="selectedItem" name="selectedCaseStudyName" (selectionChange)="onSelectionChangeHandler($event)" [ngStyle]="{'background-color':'silver'}">

        <mat-option *ngFor="let item of myItemList" [value]="item.ItemName">
            {{item.ItemName}}
        </mat-option>
    </mat-select>`        

In .ts,

'selectedItem = myItemList[2].ItemName'

myItemList is list of objects with one of the fields being ItemName. Important point is to bind [(ngModel)] to variable selectedItem and then initialize it with the required index of the list. Whenever you bind [(ngModule)], make sure that you import NgModule

import {ngModule} from '@angular/core';
Ishwara Bhat
  • 186
  • 1
  • 9