📅  最后修改于: 2022-03-11 15:02:15.755000             🧑  作者: Mango
@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...
}
}
}