我一直在尝试实现Angular 5+指令,该指令限制用户只能输入浮点数/整数。
以下是使用 input mask 的实现,该实现是我从stackoverflow获得的,但无法正常工作,因为它允许用户输入两位小数,这是错误的。 Link
import {Directive, ElementRef, Input} from '@angular/core';
import * as Inputmask from 'inputmask';
@Directive({
selector: '[app-restrict-input]',
})
export class RestrictInputDirective {
// map of some of the regex strings I'm using (TODO: add your own)
private regexMap = {
integer: '^[0-9]*$',
float: '^[+-]?([0-9]*[.])?[0-9]+$',
words: '([A-z]*\\s)*',
point25: '^\-?[0-9]*(?:\\.25|\\.50|\\.75|)$'
};
constructor(private el: ElementRef) {}
@Input('app-restrict-input')
public set defineInputType(type: string) {
Inputmask({regex: this.regexMap[type], placeholder: ''})
.mask(this.el.nativeElement);
}
}
我已经尝试实施了一个星期,但没有结果。任何帮助将不胜感激!