如何在我的 FormControl 中显示一个值并保留另一个值?

How to display a value and keep another value in my FormControl?

我正在使用 text-mask 库,它运行良好。

考虑以下 mask 的配置:

priceMask = Object.freeze({
  mask: createNumberMask({
    allowDecimal: true,
    decimalSymbol: ',',
    integerLimit: 7,
    prefix: '',
    thousandsSeparatorSymbol: '.'
  })
});

在我的 HTML 中,我有以下内容:

<form [formGroup]="formGroup">
  <input type="text"
         formControlName="maskedInput"
         [textMask]="priceMask">
</form>

您可能已经注意到,在我的掩码配置中,我将字段限制为具有如下值:

9.999.999,99

但是,虽然我想向用户显示这种特定格式,但我想在我的 control 中获得不同的值,例如:

9999999,99

这可能吗?

我希望问题足够清楚。谢谢。

这是我创建的 plnkr 来说明情况。

我会为此创建一个指令:

@Directive({
  selector: '[numeric]'
})

export class NumericDirective {

  constructor(private model: NgControl) { }

  @HostListener('input') inputChange() {
    const newValue = this.model.value.replace(/\./g, '');
    this.model.control.setValue(newValue);
    this.model.valueAccessor.writeValue(newValue);
  }    
}

并且在 HTML 中,只包含 numeric 属性:

<form [formGroup]="formGroup">
  <input type="text"
         formControlName="maskedInput"
         [textMask]="priceMask"
         numeric>
</form>

DEMO