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

Commit 43cf54c

Browse files
committed
docs(ngModelController): improve $formatters and $parsers info
Closes #11714 Closes #8194
1 parent 15149c1 commit 43cf54c

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

src/ng/directive/ngModel.js

+43-17
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,57 @@ var ngModelMinErr = minErr('ngModel');
3131
* @property {*} $viewValue The actual value from the control's view. For `input` elements, this is a
3232
* String. See {@link ngModel.NgModelController#$setViewValue} for information about when the $viewValue
3333
* is set.
34+
*
3435
* @property {*} $modelValue The value in the model that the control is bound to.
36+
*
3537
* @property {Array.<Function>} $parsers Array of functions to execute, as a pipeline, whenever
36-
the control reads value from the DOM. The functions are called in array order, each passing
37-
its return value through to the next. The last return value is forwarded to the
38-
{@link ngModel.NgModelController#$validators `$validators`} collection.
38+
* the control updates the ngModelController with a new {@link ngModel.NgModelController#$viewValue
39+
`$viewValue`} from the DOM, usually via user input.
40+
See {@link ngModel.NgModelController#$setViewValue `$setViewValue()`} for a detailed lifecycle explanation.
41+
Note that the `$parsers` are not called when the bound ngModel expression changes programmatically.
3942
40-
Parsers are used to sanitize / convert the {@link ngModel.NgModelController#$viewValue
41-
`$viewValue`}.
43+
The functions are called in array order, each passing
44+
its return value through to the next. The last return value is forwarded to the
45+
{@link ngModel.NgModelController#$validators `$validators`} collection.
4246
43-
Returning `undefined` from a parser means a parse error occurred. In that case,
44-
no {@link ngModel.NgModelController#$validators `$validators`} will run and the `ngModel`
45-
will be set to `undefined` unless {@link ngModelOptions `ngModelOptions.allowInvalid`}
46-
is set to `true`. The parse error is stored in `ngModel.$error.parse`.
47+
Parsers are used to sanitize / convert the {@link ngModel.NgModelController#$viewValue
48+
`$viewValue`}.
49+
50+
Returning `undefined` from a parser means a parse error occurred. In that case,
51+
no {@link ngModel.NgModelController#$validators `$validators`} will run and the `ngModel`
52+
will be set to `undefined` unless {@link ngModelOptions `ngModelOptions.allowInvalid`}
53+
is set to `true`. The parse error is stored in `ngModel.$error.parse`.
54+
55+
This simple example shows a parser that would convert text input value to lowercase:
56+
* ```js
57+
* function parse(value) {
58+
* if (value) {
59+
* return value.toLowerCase();
60+
* }
61+
* }
62+
* ngModelController.$parsers.push(parse);
63+
* ```
4764
4865
*
4966
* @property {Array.<Function>} $formatters Array of functions to execute, as a pipeline, whenever
50-
the model value changes. The functions are called in reverse array order, each passing the value through to the
51-
next. The last return value is used as the actual DOM value.
52-
Used to format / convert values for display in the control.
67+
the bound ngModel expression changes programmatically. The `$formatters` are not called when the
68+
value of the control is changed by user interaction.
69+
70+
Formatters are used to format / convert the {@link ngModel.NgModelController#$modelValue
71+
`$modelValue`} for display in the control.
72+
73+
The functions are called in reverse array order, each passing the value through to the
74+
next. The last return value is used as the actual DOM value.
75+
76+
This simple example shows a formatter that would convert the model value to uppercase:
77+
5378
* ```js
54-
* function formatter(value) {
79+
* function format(value) {
5580
* if (value) {
5681
* return value.toUpperCase();
5782
* }
5883
* }
59-
* ngModel.$formatters.push(formatter);
84+
* ngModel.$formatters.push(format);
6085
* ```
6186
*
6287
* @property {Object.<string, function>} $validators A collection of validators that are applied
@@ -764,9 +789,10 @@ NgModelController.prototype = {
764789
*
765790
* When `$setViewValue` is called, the new `value` will be staged for committing through the `$parsers`
766791
* and `$validators` pipelines. If there are no special {@link ngModelOptions} specified then the staged
767-
* value sent directly for processing, finally to be applied to `$modelValue` and then the
768-
* **expression** specified in the `ng-model` attribute. Lastly, all the registered change listeners,
769-
* in the `$viewChangeListeners` list, are called.
792+
* value is sent directly for processing through the `$parsers` pipeline. After this, the `$validators` and
793+
* `$asyncValidators` are called and the value is applied to `$modelValue`.
794+
* Finally, the value is set to the **expression** specified in the `ng-model` attribute and
795+
* all the registered change listeners, in the `$viewChangeListeners` list are called.
770796
*
771797
* In case the {@link ng.directive:ngModelOptions ngModelOptions} directive is used with `updateOn`
772798
* and the `default` trigger is not listed, all those actions will remain pending until one of the

0 commit comments

Comments
 (0)