首页 > 解决方案 > ngx-mask 接受负数

问题描述

我们正在使用来自 ngx-mask 的 MaskDirective 和 MaskService,以使用户接受以下格式的输入货币:000000.00。

以下是我的指示。我正在使用输入类型 =“十进制”。现在我有一个场景,其中该指令应该接受一个负数(如果有的话)。

我该如何实施?尝试使用“specialCharacters”,但我无法通过。

非常感谢任何帮助。提前致谢。

`@Directive({
  selector: 'input[type=decimal]',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => DecimalDirective),
      multi: true,
    },
    MaskService,
  ],
})
export class DecimalDirective extends MaskDirective implements OnInit {
  @HostBinding('type') public type = 'text';

  ngOnInit() {
    this.maskExpression = '099999.00';
    this.dropSpecialCharacters = false;
  }
}`

用法:

`<input type="decimal" name="sampleAmount" formControlName="sampleAmount" class="form-control" placeholder="0.00">`

标签: angular

解决方案


<input mask="V0*.00" 
      [patterns]="customPatterns" 
      [dropSpecialCharacters]="false">

customPatterns = {
    'V': {pattern: new RegExp('-?')},
    '0': {pattern: new RegExp('[0-9]')}
  }

参考


推荐阅读