How to set default value in ng-options
Asked Answered
T

4

15

I can set a dropdown list with default value in angularjs as,

 <select name="repeatSelect" id="repeatSelect" ng-model="repeatSelect" ng-init=" repeatSelect = data[0].id">
    <option ng-repeat="option in data" value="{{option.id}}">{{option.name}}</option>
 </select>

How can I achieve the same using ng-options? I treid with,

 <select name="repeatSelect" 
   id="repeatSelect" 
   ng-model="repeatSelect"
   ng-init=" repeatSelect = option.id"  
   ng-options="option.name for option in data track by option.id">
</select>

But no use. Sample fiddle is here

Tetanus answered 30/11, 2015 at 7:29 Comment(0)
W
23

Use ng-init to set default value for ng-options.

Here is the: demo

<select name="repeatSelect" 
   id="repeatSelect" 
   ng-model="repeatSelect"
   ng-init=" repeatSelect = data[0].id"  
   ng-options="option.id as option.name for option in data">          
</select>
Whisper answered 30/11, 2015 at 7:44 Comment(0)
W
10

All this miss use of ng-init.

From the angular docs:

This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting. Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope.

Source docs

In my opinion the correct way to set a default value is to simply pre-fill your ng-model property with the value selected from your ng-options, angular does the rest.

Essentially when you define the $scope property your select will bind to assign it the default value from your data array. If your data array is from an ajax request, just assign it once you have the data.

.controller('test', ['$scope', function ($scope) {
    $scope.data = [{name: 'one', id: 1}, {name: 'two', id: 2},{name: 'three', id: 3}];
    $scope.repeatSelect= $scope.data[0];
}]);

There is one caveat to note. If you employ the as key word in your expression you have to assign your ng-model with the actual property your telling it to select.

See full fiddle demoing both: http://jsfiddle.net/kb99gee8/

Wonted answered 30/11, 2015 at 8:5 Comment(4)
I'd also like to know that, since I do it like this. +1Eppes
@klskl i think it was just someone not liking being called out for wrong use of nginit. I can't think of any other reason, even the docs says it should only be used for a couple of use cases which doesn't include this.Wonted
what if the ng-model value is not present in ng-options? it will show empty.Investigation
@Investigation that is correct and some could argue an error case to be handled as such. It is to be expected you would perform validation to ensure the model your setting exists within the available options, if it doesn't, handle the error. Maybe by showing some form of validation error message. Ahh the nostalgia, not looked AngularJS code for ages. Angular was much nicer and React nicer still. Forgot writing this answer TBH.Wonted
B
2

i have acheieved what you need using your code and ng-options like you mentioned, here is Working Fiddle

Full CODE

<div ng-app="ngrepeatSelect">
    <div ng-controller="ExampleController">
        <form name="myForm">
            <label for="repeatSelect">Angular select:</label>
            <select name="repeatSelect" 
   id="repeatSelect" 
   ng-model="repeatSelect"
   ng-init=" repeatSelect = data[0]"  
   ng-options="option.name for option in data track by option.id">
</select>
        </form>
        <br/> <tt>Selected value: {{repeatSelect.id}}</tt>

    </div>
</div>
<br/>
Bove answered 30/11, 2015 at 7:54 Comment(1)
this is assuming your first item is "- Select - ". What if you dont have that option? Should I manually add it anyway?One
A
1

I will suggest an example HTML part

<select class="form-control" ng-model="data.selectedOption" required ng-options="option.name as option.name for option in venueNamelists"  ng-change="getmediationRooms(data.selectedOption)"></select>

In my controller

$scope.data = {selectedOption: $scope.venueNamelists[i].name};

the model value should equal to the key,value pair in the venueNamelists.

Alisha answered 12/5, 2017 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.