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

Commit 5b18250

Browse files
docs(select): provide more info and example about non-string options
See #11890
1 parent f26fc26 commit 5b18250

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

src/ng/directive/select.js

+46-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ var SelectController =
112112
* ngOptions} to achieve a similar result. However, `ngOptions` provides some benefits such as reducing
113113
* memory and increasing speed by not creating a new scope for each repeated instance, as well as providing
114114
* more flexibility in how the `<select>`'s model is assigned via the `select` **`as`** part of the
115-
* comprehension expression. `ngOptions` should be used when the `<select>` model needs to be bound
116-
* to a non-string value. This is because an option element can only be bound to string values at
117-
* present.
115+
* comprehension expression.
118116
*
119117
* When an item in the `<select>` menu is selected, the array element or object property
120118
* represented by the selected option will be bound to the model identified by the `ngModel`
@@ -127,6 +125,51 @@ var SelectController =
127125
* be nested into the `<select>` element. This element will then represent the `null` or "not selected"
128126
* option. See example below for demonstration.
129127
*
128+
* <div class="alert alert-info">
129+
* The value of a `select` directive used without `ngOptions` is always a string.
130+
* When the model needs to be bound to a non-string value, you must either explictly convert it
131+
* using a directive (see example below) or use `ngOptions` to specify the set of options.
132+
* This is because an option element can only be bound to string values at present.
133+
* </div>
134+
*
135+
* ### Example (binding `select` to a non-string value)
136+
*
137+
* <example name="select-with-non-string-options" module="nonStringSelect">
138+
* <file name="index.html">
139+
* <select ng-model="model.id" convert-to-number>
140+
* <option value="0">Zero</option>
141+
* <option value="1">One</option>
142+
* <option value="2">Two</option>
143+
* </select>
144+
* {{ model }}
145+
* </file>
146+
* <file name="app.js">
147+
* angular.module('nonStringSelect', [])
148+
* .run(function($rootScope) {
149+
* $rootScope.model = { id: 2 };
150+
* })
151+
* .directive('convertToNumber', function() {
152+
* return {
153+
* require: 'ngModel',
154+
* link: function(scope, element, attrs, ngModel) {
155+
* ngModel.$parsers.push(function(val) {
156+
* return parseInt(val, 10);
157+
* });
158+
* ngModel.$formatters.push(function(val) {
159+
* return '' + val;
160+
* });
161+
* }
162+
* };
163+
* });
164+
* </file>
165+
* <file name="protractor.js" type="protractor">
166+
* it('should initialize to model', function() {
167+
* var select = element(by.css('select'));
168+
* expect(element(by.model('model.id')).$('option:checked').getText()).toEqual('Two');
169+
* });
170+
* </file>
171+
* </example>
172+
*
130173
*/
131174
var selectDirective = function() {
132175

0 commit comments

Comments
 (0)