Skip to content

Commit 998340d

Browse files
committed
docs(select): correct workaround for numeric option bc
1 parent 5593136 commit 998340d

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

CHANGELOG.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -1223,9 +1223,19 @@ In Angular 1.3.x, setting `scope.x = 200` would select the `option` with the val
12231223
</select>
12241224
```
12251225

1226-
In Angular 1.4.x, the 'unknown option' will be selected. To remedy this, you can either initialize
1227-
the model as a string - `scope.x = '200'` - or implement a parser on `ngModel` that converts
1228-
the option string value to a `Number`.
1226+
In Angular 1.4.x, the 'unknown option' will be selected.
1227+
To remedy this, you can simply initialize the model as a string: `scope.x = '200'`, or if you want to
1228+
keep the model as a `Number`, you can do the conversion via `$formatters` and `$parsers` on `ngModel`:
1229+
1230+
```js
1231+
ngModelCtrl.$parsers.push(function(value) {
1232+
return parseInt(value, 10); // Convert option value to number
1233+
});
1234+
1235+
ngModelCtrl.$formatters.push(function(value) {
1236+
return value.toString(); // Convert scope value to string
1237+
});
1238+
```
12291239

12301240
<a name="1.3.9"></a>
12311241
# 1.3.9 multidimensional-awareness (2015-01-13)

docs/content/guide/migration.ngdoc

+15-5
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ have been fixed. The breaking changes are comparatively minor and should not aff
178178
Due to [7fda214c](https://github.com/angular/angular.js/commit/7fda214c4f65a6a06b25cf5d5aff013a364e9cef),
179179
when `ngOptions` renders the option values within the DOM, the resulting HTML code is different.
180180
Normally this should not affect your application at all, however, if your code relies on inspecting
181-
the value property of `<option>` elements (that `ngOptions` generates) then be sure to [read the details]
182-
(https://github.com/angular/angular.js/commit/7fda214c4f65a6a06b25cf5d5aff013a364e9cef).
181+
the value property of `<option>` elements (that `ngOptions` generates) then be sure
182+
to [read the details](https://github.com/angular/angular.js/commit/7fda214c4f65a6a06b25cf5d5aff013a364e9cef).
183183

184184
Due to [7fda214c](https://github.com/angular/angular.js/commit/7fda214c4f65a6a06b25cf5d5aff013a364e9cef),
185185
when iterating over an object's properties using the `(key, value) in obj` syntax
@@ -205,9 +205,19 @@ In Angular 1.3.x, setting `scope.x = 200` would select the option with the value
205205
</select>
206206
```
207207

208-
In Angular 1.4.x, the 'unknown option' will be selected. To remedy this, you can either initialize
209-
the model as a string - `scope.x = '200'` - or implement a parser on `ngModel` that converts
210-
the option string value to a `Number`.
208+
In Angular 1.4.x, the 'unknown option' will be selected.
209+
To remedy this, you can simply initialize the model as a string: `scope.x = '200'`, or if you want to
210+
keep the model as a `Number`, you can do the conversion via `$formatters` and `$parsers` on `ngModel`:
211+
212+
```js
213+
ngModelCtrl.$parsers.push(function(value) {
214+
return parseInt(value, 10); // Convert option value to number
215+
});
216+
217+
ngModelCtrl.$formatters.push(function(value) {
218+
return value.toString(); // Convert scope value to string
219+
});
220+
```
211221

212222
## Templating (`ngRepeat`, `$compile`)
213223

0 commit comments

Comments
 (0)