I have some custom validation code, which includes a $formatter. (I store currency in pence for correctness, but display in pounds.pence.)
If the user types \'10\'
A little improved: Do not reformat if the value is not valid (in my case invalid text just got cleared on blur, which is bad for usability, I think).
Also, like Dark Falcon said: Formatters should be iterated backwards.
Finally do not iterate over arrays with for-in, at least not without checking hasOwnProperty() (for me the code crashed because it treated Array.find() as a formatter).
// Reformat text on blur
elements.bind('blur', function() {
if(!ngModel.$valid) {
return;
}
var viewValue = ngModel.$modelValue;
var formatters = ngModel.$formatters;
for (var i = formatters.length - 1; i >= 0; --i) {
viewValue = formatters[i](viewValue);
}
ngModel.$viewValue = viewValue;
ngModel.$render();
});