I think it's best to keep your model values as numbers instead of strings if it makes sense from a data modelling perspective. That way you can do sums, comparisons etc more easily even in places were you don't have enough control to write javascript conversion code, like an expression:
<div ng-show="project.cible + project.someOtherNum > 0">I'm visible</div>
This can be done by changing the input type to type="number"
. Angular has a special directive for this case which (among other things) adds a parser/formatter in the associated ngModelController
:
ctrl.$parsers.push(function(value) {
var empty = ctrl.$isEmpty(value);
if (empty || NUMBER_REGEXP.test(value)) {
ctrl.$setValidity('number', true);
return value === '' ? null : (empty ? value : parseFloat(value));
} else {
ctrl.$setValidity('number', false);
return undefined;
}
});
ctrl.$formatters.push(function(value) {
return ctrl.$isEmpty(value) ? '' : '' + value;
});
Valid input is converted to float prior to updating the model property, and model properties are converted to strings prior to updating the DOM element.
0
instead"0"