Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3d6dc3f

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 #12546
1 parent 0c81e9f commit 3d6dc3f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/ng/directive/select.js

+50
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ var SelectController =
108108
* @description
109109
* HTML `SELECT` element with angular data-binding.
110110
*
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
@@ -125,6 +140,41 @@ var SelectController =
125140
* be nested into the `<select>` element. This element will then represent the `null` or "not selected"
126141
* option. See example below for demonstration.
127142
*
143+
* ### Example use of select with default value set
144+
*
145+
* <example name="select-with-default-values" module="defaultValueSelect">
146+
* <file name="index.html">
147+
* <div ng-controller="ExampleController">
148+
* <form name="myForm">
149+
* <label for="mySelect">Make a choice:</label>
150+
* <select name="mySelect" id="mySelect"
151+
* ng-options="option.name for option in data.availableOptions track by option.id"
152+
* ng-model="data.selectedOption"></select>
153+
* </form>
154+
* <hr>
155+
* <tt>option = {{data.selectedOption}}</tt><br/>
156+
* <tt>myForm.mySelect.$valid = {{myForm.mySelect.$valid}}</tt><br/>
157+
* <tt>myForm.mySelect.$error = {{myForm.mySelect.$error}}</tt><br/>
158+
* <tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
159+
* <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
160+
* </div>
161+
* </file>
162+
* <file name="app.js">
163+
* angular.module('defaultValueSelect', [])
164+
* .controller('ExampleController', ['$scope', function($scope) {
165+
* $scope.data = {
166+
* availableOptions: [
167+
* {id: '1', name: 'Option A'},
168+
* {id: '2', name: 'Option B'},
169+
* {id: '3', name: 'Option C'}
170+
* ],
171+
* selectedOption: {id: '3', name: 'Option C'} //This sets the default value of the select in the ui
172+
* };
173+
* }]);
174+
* </file>
175+
*</example>
176+
*
177+
*
128178
* <div class="alert alert-info">
129179
* The value of a `select` directive used without `ngOptions` is always a string.
130180
* When the model needs to be bound to a non-string value, you must either explictly convert it

0 commit comments

Comments
 (0)