我正在尝试添加。我的目的是添加一个具有数千个点和十进制逗号(例如1.900.235,65)的蒙版。问题是我不知道可以输入多少位数。问题是primeNg的p-inputMask不允许正则表达式控制(我认为)。有什么办法可以解决这个问题?
答案 0 :(得分:0)
您可以创建您的自定义指令,并将其与正则表达式一起使用,例如,此代码以三元组(1000000到1000000)格式化数字字符串
@Directive({
selector: '[appFormatprice]'
})
export class FormatpriceDirective implements OnInit {
constructor(private el: ElementRef, private control: NgControl) { }
@HostListener('keyup', ['$event'])
@HostListener('paste', ['$event'])
@HostListener('input', ['$event'])
onKeyUp(event) {
let val = this.formatPrice(this.control.value);
this.control.control.setValue(val);
}
onPaste(event) {
let val = this.formatPrice(this.control.value);
this.control.control.setValue(val);
}
ngOnInit() {
if (this.control.value) {
let val = this.formatPrice(this.control.value);
this.control.control.setValue(val);
}
}
formatPrice(str) { return str && str.toString().replace(/\D/g, '').replace(/\s(?=\d)/g, '').replace(/(\d)(?=(\d{3})+(\.|$|\D))/g, '$1 ') || ''; }
}