I have a directive which manipulates the $viewValue of an input field via ngModelCtrl.$setViewValue() on blur.
function _onFocus () {
presentation = false;
if(!ctrl.$isEmpty(ctrl.$modelValue)) {
ctrl.$setViewValue(String(ctrl.$modelValue).replace(/\./, ','));
ctrl.$render();
}
}
Plunkr
Since this is only an appearance thing, I wonder if there is the possibility of setting the ViewValue without altering the $pristine/$dirty state of the input field itself.
Reference Angular: $setDirty on $commitViewValue
I would probably set the value
of the element instead of setting view value and rending all over, since it is just the appearance thing that happens on manually registered event, and you do not have to play with the ngmodel's validation properties.
i.e:
function _onBlur () {
//....
$element.val(_formatCurrency(ctrl.$modelValue));
}
function _onFocus () {
//...
$element.val(String(ctrl.$modelValue).replace(/\./, ','));
}
Plnkr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With