Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngModelCtrl - $setViewValue without $setDirty();

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

like image 704
strangfeld Avatar asked Oct 20 '22 18:10

strangfeld


1 Answers

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

like image 88
PSL Avatar answered Oct 27 '22 10:10

PSL