forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(error/ngModel/numfmt): provide documentation for this error
See: angular@db044c4#commitcomment-7577199 Closes angular#11157 Closes angular#11334
- Loading branch information
1 parent
4588e62
commit 7c9ad27
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
@ngdoc error | ||
@name ngModel:numfmt | ||
@fullName Model is not of type `number` | ||
@description | ||
|
||
The number input directive `<input type="number">` requires the model to be a `number`. | ||
|
||
If the model is something else, this error will be thrown. | ||
|
||
Angular does not set validation errors on the `<input>` in this case | ||
as this error is caused by incorrect application logic and not by bad input from the user. | ||
|
||
If your model does not contain actual numbers then it is up to the application developer | ||
to use a directive that will do the conversion in the `ngModel` `$formatters` and `$parsers` | ||
pipeline. | ||
|
||
## Example | ||
|
||
In this example, our model stores the number as a string, so we provide the `stringToNumber` | ||
directive to convert it into the format the `input[number]` directive expects. | ||
|
||
|
||
<example module="numfmt-error-module"> | ||
<file name="index.html"> | ||
<table> | ||
<tr ng-repeat="x in ['0', '1']"> | ||
<td> | ||
<input type="number" string-to-number ng-model="x" /> {{ x }} : {{ typeOf(x) }} | ||
</td> | ||
</tr> | ||
</table> | ||
</file> | ||
<file name="app.js"> | ||
angular.module('numfmt-error-module', []) | ||
|
||
.run(function($rootScope) { | ||
$rootScope.typeOf = function(value) { | ||
return typeof value; | ||
}; | ||
}) | ||
|
||
.directive('stringToNumber', function() { | ||
return { | ||
require: 'ngModel', | ||
link: function(scope, element, attrs, ngModel) { | ||
ngModel.$parsers.push(function(value) { | ||
return '' + value; | ||
}); | ||
ngModel.$formatters.push(function(value) { | ||
return parseFloat(value, 10); | ||
}); | ||
} | ||
}; | ||
}); | ||
</file> | ||
</example> |