ngModelCtrl - 没有$ setDirty()的$ setViewValue;

时间:2015-02-01 17:04:25

标签: javascript angularjs angularjs-directive angular-ngmodel

我有一个指令,它通过ngModelCtrl操作输入字段的$ viewValue。$ setViewValue()模糊。

function _onFocus () {
    presentation = false;
    if(!ctrl.$isEmpty(ctrl.$modelValue)) {
      ctrl.$setViewValue(String(ctrl.$modelValue).replace(/\./, ','));
      ctrl.$render();
    }
  }

Plunkr

由于这只是外观,我想知道是否有可能设置ViewValue而不改变输入字段本身的$ pristine / $ dirty状态。

参考角度:$setDirty on $commitViewValue

1 个答案:

答案 0 :(得分:3)

我可能会设置元素的value而不是设置视图值并全部撕掉,因为它只是在手动注册的事件上发生的外观事件,并且您不必使用ngmodel& #39;验证属性。

即:

  function _onBlur () {
     //....
      $element.val(_formatCurrency(ctrl.$modelValue));
  }

  function _onFocus () {
     //...
     $element.val(String(ctrl.$modelValue).replace(/\./, ','));
  }

<强> Plnkr