Skip to content

Commit 0e7546c

Browse files
meliaxfordNarretz
authored andcommitted
docs(select): explain how to set default value
Setting the default value in a select is a real trap for beginners, questions about how to do this on StackExchange have been view more than 40000 times in the last year. This changes updates the documentation to make it clearer. Closes angular#12546
1 parent 3bcda7c commit 0e7546c

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

src/ng/directive/select.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,22 @@ var SelectController =
107107
*
108108
* @description
109109
* HTML `SELECT` element with angular data-binding.
110-
*
110+
*
111+
* The `select` directive provides support for {@link ngModel.NgModelController NgModelController} to
112+
* read and write the selected value(s) of the '<select>' control (including the default value) and
113+
* coordinates dynamically added `<option>` elements, which can be added using the `ngRepeat` or
114+
* 'ngOptions' directive.
115+
*
116+
* @param {string} ngModel Assignable angular expression to data-bind to. Also used to set the default
117+
* value of the select by assigning the desired value to this model in scope.
118+
* @param {string=} name Property name of the form under which the control is published.
119+
* @param {string=} required Sets `required` validation error key if the value is not entered.
120+
* @param {boolean=} ngRequired Sets `required` attribute if set to true
121+
* @param {string=} ngChange Angular expression to be executed when selected option(s) changes due to user
122+
* interaction with the select element.
123+
* @param {string=} ngOptions sets the options that the select is populated with. Alternatively the options
124+
* can be added with the '<option>' and the 'ngRepeat' directive.
125+
*
111126
* In many cases, `ngRepeat` can be used on `<option>` elements instead of {@link ng.directive:ngOptions
112127
* ngOptions} to achieve a similar result. However, `ngOptions` provides some benefits such as reducing
113128
* memory and increasing speed by not creating a new scope for each repeated instance, as well as providing
@@ -124,6 +139,40 @@ var SelectController =
124139
* Optionally, a single hard-coded `<option>` element, with the value set to an empty string, can
125140
* be nested into the `<select>` element. This element will then represent the `null` or "not selected"
126141
* option. See example below for demonstration.
142+
*
143+
* ### Example use of select with default value set
144+
*
145+
<script>
146+
angular.module('selectExample', [])
147+
.controller('ExampleController', ['$scope', function($scope) {
148+
$scope.data = {
149+
availableOptions: [
150+
{id: '1', name: 'Option A'},
151+
{id: '2', name: 'Option B'},
152+
{id: '3', name: 'Option C'}
153+
],
154+
selectedOption: {id: '1', name: 'Option A'} //This sets the default value of the select in the ui
155+
};
156+
}]);
157+
</script>
158+
<div ng-controller="ExampleController">
159+
<form name="myForm">
160+
<label>
161+
Make a choice: </label>
162+
<select name="mySelect" ng-options="option.name for option in data.availableOptions track by option.id" ng-model="data.selectedOption" required></select>
163+
<div role="alert">
164+
<span class="error" ng-show="myForm.userName.$error.required">
165+
Required!</span>
166+
</div>
167+
</form>
168+
<hr>
169+
<tt>option = {{data.selectedOption}}</tt><br/>
170+
<tt>myForm.mySelect.$valid = {{myForm.mySelect.$valid}}</tt><br/>
171+
<tt>myForm.mySelect.$error = {{myForm.mySelect.$error}}</tt><br/>
172+
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
173+
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
174+
</div>
175+
*
127176
*
128177
* <div class="alert alert-info">
129178
* The value of a `select` directive used without `ngOptions` is always a string.

0 commit comments

Comments
 (0)