📜  在角度中添加分数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:15.755000             🧑  作者: Mango

代码示例1
@Directive({
   selector: '[exampleDirective]'
})
export class TestDirective {

   constructor() {}

   @HostListener('input', ['$event'])
   ngOnChanges(evt: any) {
      const pattern: RegExp = new RegExp(/^[0-9]+\.?[0-9]*$/);
      if (!pattern.test(evt.target.value)) {
         evt.srcElement.value = evt.srcElement.value.substring(0, evt.srcElement.value.length - 1); // this will erase the last char that does not match the pattern...
      }
   }
}