Javascript 支持十进制数的Number指令

Javascript 支持十进制数的Number指令,javascript,angular,typescript,angular2-directives,Javascript,Angular,Typescript,Angular2 Directives,我已经为文本输入编写了一个指令,以支持int值 给你 import { NgControl } from '@angular/forms'; import { HostListener, Directive } from '@angular/core'; @Directive({ exportAs: 'number-directive', selector: 'number-directive, [number-directive]' }) export class NumberDir

我已经为文本输入编写了一个指令,以支持int值

给你

import { NgControl } from '@angular/forms';
import { HostListener, Directive } from '@angular/core';

@Directive({
  exportAs: 'number-directive',
  selector: 'number-directive, [number-directive]'
})
export class NumberDirective {
  private el: NgControl;
  constructor(ngControl: NgControl) {
    this.el = ngControl;
  }
  // Listen for the input event to also handle copy and paste.
  @HostListener('input', ['$event.target.value'])
  onInput(value: string) {
    // Use NgControl patchValue to prevent the issue on validation
    this.el.control.patchValue(value.replace(/[^0-9]/g, ''));
  }
}
和HTML

 <div class="form-group">
                    <label>{{ l("RoomWidth") }}</label>
                    <input
                        decimal-number-directive
                        #roomWidthInput="ngModel"
                        class="form-control nospinner-input"
                        type="text"
                        name="roomWidth"
                        [(ngModel)]="room.roomWidth"
                        maxlength="32"
                    />
                </div>

{{l(“房间宽度”)}
但我需要它来支持十进制值。例如99.5

我需要如何修改它?

尝试以下操作:

@HostListener('input', ['$event.target.value'])
onInput(value: string) {
  // Use NgControl patchValue to prevent the issue on validation
  this.el.control.patchValue(value.replace(/[^0-9].[^0-9]/g, ''));
}
试试这个:

@HostListener('input', ['$event.target.value'])
onInput(value: string) {
  // Use NgControl patchValue to prevent the issue on validation
  this.el.control.patchValue(value.replace(/[^0-9].[^0-9]/g, ''));
}

在我的情况下,我还需要考虑两个约束点,并替换字符。也许可以用正则表达式写,但这对我来说很有用。 例如,如果键入或粘贴45.456.6,将使用45.4566重新绘制

 @HostListener('input', ['$event']) onInputChange(event) {
    let initalValue: string = this.el.nativeElement.value;
    initalValue = initalValue.replace(/[^\.|0-9]/g, '');
    // elimina le seconde occorrenze del punto
    const count = (initalValue.match(/\./g) || []).length;
    for (let i = 1; i < count; i++) {
      initalValue = this.repaceSecondDotOccurrence(initalValue);
    }
    this.el.nativeElement.value = initalValue;
  }


  repaceSecondDotOccurrence(inputString): string {
    let t = 0;
    return inputString.replace(/\./g, function (match) {
      t++;
      return (t === 2) ? '' : match;
    });
  }
@HostListener('input',['$event'])onInputChange(event){
让initalValue:string=this.el.nativeElement.value;
initalValue=initalValue.replace(/[^\.| 0-9]/g');
//埃莉米娜·勒·奥科伦泽·德尔·普托第二酒店
常量计数=(initalValue.match(/\./g)| |[])长度;
for(设i=1;i
在我的情况下,我还需要考虑两个约束点,并替换字符。也许可以用正则表达式写,但这对我来说很有用。 例如,如果键入或粘贴45.456.6,将使用45.4566重新绘制

 @HostListener('input', ['$event']) onInputChange(event) {
    let initalValue: string = this.el.nativeElement.value;
    initalValue = initalValue.replace(/[^\.|0-9]/g, '');
    // elimina le seconde occorrenze del punto
    const count = (initalValue.match(/\./g) || []).length;
    for (let i = 1; i < count; i++) {
      initalValue = this.repaceSecondDotOccurrence(initalValue);
    }
    this.el.nativeElement.value = initalValue;
  }


  repaceSecondDotOccurrence(inputString): string {
    let t = 0;
    return inputString.replace(/\./g, function (match) {
      t++;
      return (t === 2) ? '' : match;
    });
  }
@HostListener('input',['$event'])onInputChange(event){
让initalValue:string=this.el.nativeElement.value;
initalValue=initalValue.replace(/[^\.| 0-9]/g');
//埃莉米娜·勒·奥科伦泽·德尔·普托第二酒店
常量计数=(initalValue.match(/\./g)| |[])长度;
for(设i=1;i
regex?你能提供stacblitz吗?你是用空字符串替换
0-9
数字吗?现在我用空字符串替换所有符号,只写数字@prashantpimpale请提供相关的html和Ts代码regex?你能提供stacblitz吗?你是用空字符串替换
0-9
数字吗?现在我用空字符串替换所有符号空字符串和只写数字@prashantpimpale请提供相关的html和Ts代码帮助。谢谢,还有一个小问题,我如何将
后的数字限制为2位?这很有帮助。谢谢,还有一个小问题,我如何将
后的数字限制为2位?