【问题标题】:<input matInput ... (Angular material form) Allow 3 decimal points on input, but only display 2 decimal points<input matInput ... (Angular 材料形式) 输入时允许 3 个小数点,但只显示 2 个小数点
【发布时间】:2020-10-07 20:53:11
【问题描述】:

我有一个常规的角度材质表单,它使用 numberMaskOptions 将字段值的输入和显示限制为 3 个小数点。 (见下面的代码)

这很好,但客户现在希望将字段的“显示”限制为仅显示 2 个小数点,但希望允许用户在同一字段中输入最多 3 个小数点。

换句话说,当光标不在该字段中时,它应该显示2个小数点的精度,但是当用户进入该字段进行编辑时,它应该允许3个小数点的精度。

这对材质 matInput 字段是否可行?如果有怎么办?如果不是,我还应该如何处理?

    <div *ngIf="isFieldVisible">
      <mat-form-field myAppTooltip>
        <mat-label>Insect Body Size</mat-label>
        <input
          autocomplete="off"
          appNumberMask
          formControlName="InsectBodySizeSmm"
          matInput
          max="99999"
          min="0"
          [numberMaskOptions]="threeDecPrecisionDecimalMaskOptions"
        />
        <mat-error></mat-error>
      </mat-form-field>
    </div>

带着我的面具

threeDecPrecisionDecimalMaskOptions = {
      align: 'right',
      allowNegative: false,
      decimalSeparator: '.',
      precision: 3,
      prefix: '',
      suffix: '',
      thousandsSeparator: '',
      valueMode: 'standard',
    };

这允许我在字段表单中输入和查看小数点后 3 位的值。

【问题讨论】:

    标签: html angular forms angular-material


    【解决方案1】:

    前段时间我做了一个属性指令来做一些类似的,你可以在this link看到,我不知道你是否可以使用,因为你也有一个指令来屏蔽数字。

    代码原样:

    @Directive({ selector: "[decimal2]" })
    export class Decimal2 implements OnInit {
      private el: HTMLInputElement;
      private value: any;
      constructor(private elementRef: ElementRef) {
        this.el = this.elementRef.nativeElement;
      }
      @HostListener("focus", ["$event.target.value"])
      onFocus() {
        this.el.value = this.value;
      }
    
      @HostListener("blur", ["$event.target.value"])
      onBlur(value: any) {
        this.transform(value);
      }
      ngOnInit() {
        this.transform(this.el.value);
      }
      transform(value: any) {
        this.value = value;
        if (value && !isNaN(value)) {
          const aux = "" + Math.round(+this.value * 100);
          this.el.value = aux.slice(0, -2) + "." + aux.slice(-2);
          if (this.el.value.indexOf(this.value) >= 0) this.value = this.el.value;
        }
      }
    }
    

    更新我们可以使用转换函数中的 angular formatNumber 来改进我们的指令

      transform(value: any) {
        this.value = value;
        if (value && !isNaN(value)) {
          this.el.value=formatNumber(+this.value, "en-US", '1.2-2');
          const formated=this.el.value.split(',').join('')
          if (formated.indexOf(this.value) >= 0) this.value = formated;
        }
      }
    

    【讨论】:

    • 在看这个时,它实际上并没有满足我的需要。我需要编辑该字段以允许 3 位数字,但是一旦发生 onBlur(失焦)事件,我需要该字段的显示始终达到四舍五入的 2 位精度。使用您给出的示例,它为两者保持相同的精度设置
    • 指令 Decimal2 仅模糊显示四舍五入为两位数的数字,我不知道您的 appNumberMask 并且我不知道如果您同时使用这两个指令是否存在冲突
    • 我更新了stackblitz,在stackoverflow.com/questions/54460923/… 中添加了指令,我觉得它看起来不错。请记住,您需要同时使用 两个指令
    猜你喜欢
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2022-01-23
    • 2017-03-02
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多