Bhr*_*jni 5 currency-pipe angular
我在模糊事件中使用货币管道。但它第一次工作正常,当我收到验证错误消息时,如果我删除了几个数字并出来,它不会被格式化为货币格式仍然是用户删除的格式。例如,我给出了这个数字:
36543265所以当我从输入中出来时,它被格式化为$36,543,265.00,带有验证错误消息。所以如果我265.00从 中删除
$36,543,265.00,我仍然有错误验证消息。所以验证错误消息消失并保持这种格式,$36,543但它没有达到正确的格式。USD清除验证消息后,如何使其成为正确的货币管道格式。
如何根据用户删除的值使其正确格式化。
TS:
public transformAmount(element, name) {
if (name == "amount") {
let formattedAmount = this.currencyPipe.transform(this.eoInfoForm.value.amount, 'USD');
element.target.value = formattedAmount;
this.eoInfoForm.get('amount').setValue(formattedAmount);
if (this.eoInfoForm.get('amount').status === 'VALID') {
this.amt = false;
}
else {
this.amt = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<input type="text" class="form-control" placeholder="Amount in dolars"
formControlName="amount" autocomplete="off" maxlength="8" allowNumberOnly (blur)="transformAmount($event,'amount')" [ngClass]="{ 'is-invalid': amt && eoInfo.amount.invalid }">
<div *ngIf="amt && eoInfo.amount.invalid" class="invalid-feedback">
<div *ngIf="amt && eoInfo.amount.invalid">Maximum of 8 characters including $ is allowed</div>
</div>
Run Code Online (Sandbox Code Playgroud)
演示: 演示
您可以编写一个稍微简单的指令来执行此操作:
import { Directive, HostListener, ElementRef, OnInit, Input } from "@angular/core";
import { CurrencyPipe } from '@angular/common';
@Directive({ selector: "[currencyInput]" })
export class CurrencyInputDirective implements OnInit {
// build the regex based on max pre decimal digits allowed
private regexString(max?: number) {
const maxStr = max ? `{0,${max}}` : `+`;
return `^(\\d${maxStr}(\\.\\d{0,2})?|\\.\\d{0,2})$`
}
private digitRegex: RegExp;
private setRegex(maxDigits?: number) {
this.digitRegex = new RegExp(this.regexString(maxDigits), 'g')
}
@Input()
set maxDigits(maxDigits: number) {
this.setRegex(maxDigits);
}
private el: HTMLInputElement;
constructor(
private elementRef: ElementRef,
private currencyPipe: CurrencyPipe
) {
this.el = this.elementRef.nativeElement;
this.setRegex();
}
ngOnInit() {
this.el.value = this.currencyPipe.transform(this.el.value, 'USD');
}
@HostListener("focus", ["$event.target.value"])
onFocus(value) {
// on focus remove currency formatting
this.el.value = value.replace(/[^0-9.]+/g, '')
}
@HostListener("blur", ["$event.target.value"])
onBlur(value) {
// on blur, add currency formatting
this.el.value = this.currencyPipe.transform(value, 'USD');
}
// variable to store last valid input
private lastValid = '';
@HostListener('input', ['$event'])
onInput(event) {
// on input, run regex to only allow certain characters and format
const cleanValue = (event.target.value.match(this.digitRegex) || []).join('')
if (cleanValue || !event.target.value)
this.lastValid = cleanValue
this.el.value = cleanValue || this.lastValid
}
}
Run Code Online (Sandbox Code Playgroud)
该指令将值转换为焦点上的数字,并在模糊时将值转换回格式化的货币字符串。
使用如下:
<input type="text" class="form-control" placeholder="Amount in dolars" autocomplete="off"
formControlName="amount" currencyInput maxDigits="9"
[ngClass]="{ 'is-invalid': eoInfo.amount.dirty && eoInfo.amount.invalid }">
Run Code Online (Sandbox Code Playgroud)
闪电战:https://stackblitz.com/edit/angular-csyslb ?file=src%2Fapp%2Fcurrency-input.directive.ts