我正在开发一个 Angular 项目,并且我有一个 Project 类型的对象,其中包含以下值:
成本:56896 和 费用HR:27829
我想使用表单修改对象并将字段与 ngModel 绑定到属性。
但我面临的问题是,在现场,我想以货币格式显示数字(例如 56,896€),这与 float 变量类型不兼容。
有人可以帮我解决这个问题吗?我尝试过使用内置的 Angular 管道以及自定义格式化程序和解析器函数,但它们似乎都没有按预期工作。
任何帮助将不胜感激。
import { Component, OnInit } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [CurrencyPipe]
})
export class AppComponent implements OnInit {
project = {
cost: 56896,
costRH: 27829
}
constructor(private currencyPipe: CurrencyPipe) { }
ngOnInit() {
this.project.cost = this.currencyPipe.transform(this.project.cost, 'EUR', 'symbol', '1.0-0');
this.project.costRH = this.currencyPipe.transform(this.project.costRH, 'EUR', 'symbol', '1.0-0');
}
onBlur(event, projectProperty) {
this.project[projectProperty] = this.currencyPipe.parse(event.target.value);
}
}
<form>
<label for="cost">Cost</label>
<input [(ngModel)]="project.cost" (blur)="onBlur($event, 'cost')" [ngModelOptions]="{updateOn: 'blur'}" [value]="project.cost | currency:'EUR':'symbol':'1.0-0'">
<label for="costRH">Cost RH</label>
<input [(ngModel)]="project.costRH" (blur)="onBlur($event, 'costRH')" [ngModelOptions]="{updateOn: 'blur'}" [value]="project.costRH | currency:'EUR':'symbol':'1.0-0'">
</form>
我的期望: 我希望输入字段被格式化为货币(例如 56,896 欧元),并且当输入失去焦点时,“projet”对象中的相应属性(cost 和 costRH)将使用解析的值进行更新。
发生了什么: 输入字段中显示的值未格式化为货币,并且对象中的相应属性未按预期更新。
您可以使用类似的指令
@Directive({
selector: '[format]',
})
export class FormatDirective implements OnInit {
format = 'N0';
digitsInfo = '1.0-0';
@Input() currency = '$';
@Input() suffix = '';
@Input() decimalCharacter = null;
@Input('format') set _(value: string) {
this.format = value;
if (this.format == 'N2') this.digitsInfo = '1.2-2';
const parts = value.split(':');
if (parts.length > 1) {
this.format = value[0];
this.digitsInfo = parts[1];
}
}
@HostListener('blur', ['$event.target']) blur(target: any) {
target.value = this.parse(target.value);
}
@HostListener('focus', ['$event.target']) focus(target: any) {
target.value = this.control.value;
}
ngOnInit() {
setTimeout(() => {
this.el.nativeElement.value = this.parse(this.el.nativeElement.value);
});
}
constructor(
@Inject(LOCALE_ID) private locale: string,
private el: ElementRef,
private control: NgControl
) {}
parse(value: any) {
let newValue = value;
if (this.format == 'C2')
newValue = formatCurrency(value, this.locale, this.currency);
if (this.format == 'N2')
newValue = formatNumber(value, this.locale, this.digitsInfo);
if (this.format == 'N0')
newValue = formatNumber(value, this.locale, this.digitsInfo);
if (this.format == 'NX')
newValue = formatNumber(value, this.locale, this.digitsInfo);
if (this.decimalCharacter)
return (
newValue.replace('.', 'x').replace(',', '.').replace('x', ',') +
this.suffix
);
return newValue + this.suffix;
}
}
工作原理就像
<input format="N0" [(ngModel)]="value"/>
<input format="N2" [(ngModel)]="value"/>
<input format="C2" [(ngModel)]="value"/>
<input format="N2" decimalCharacter='.' suffix=' €' [(ngModel)]="value"/>
该指令的想法是,当模糊时,更改“本机元素”,但不更改 ngControl 的值,当焦点返回 ngControl 的值时