angular - 如何在我的 formControl 中显示逗号后的 2 位数字
问题描述
例如,我试图让我的输入具有像“1,00”这样的值。但是,我的方法是错误的,它在输入中显示“100”。
这是我的html代码
<mat-form-field appearance="outline">
<mat-label>valor unitário</mat-label>
<input matInput formControlName="valor" prefix="R$ " mask="separator.2" thousandSeparator="." (blur)="blurValueInput('valor')" (keyup)="autoComplete('valor')">
<mat-error>campo obrigatório</mat-error>
</mat-form-field>
这是我的打字稿代码。如果我让输入具有这样的值“1,0”,它应该显示“1,00”
constructor(private form: FormBuilder) { }
ngOnInit() {
this.pagamentoForm = this.form.group({
quantidade: [1, Validators.required],
desc_produto: ['', Validators.required],
valor: ['', Validators.required],
desc_iti: [''],
desc_varejo: ['']
});
this.descInput.nativeElement.focus();
}
public blurValueInput(labelName: string) {
if (this.pagamentoForm.controls[labelName].value.length -
this.pagamentoForm.controls[labelName].value.indexOf('.') === 2) {
this.pagamentoForm.controls[labelName].setValue(Number.parseFloat(this.pagamentoForm.controls[labelName].value).toFixed(2));
}
}
解决方案
推荐阅读
- sql - SQL join - 如何选择出现在一个表中而不是另一个表中的所有不同值
- c# - 玩家跳但不下来
- javascript - HTML 按钮隐藏无法通过单击正常工作
- c# - MigraDoc 文本溢出到下一页页眉
- java - 删除 searchView 中单词的重音
- python-3.x - 在 __getitem() 中使用切片
- php - 如何在 PHP 5.4 版本中对多维数组进行排序?
- android - 禁用 RecyclerView 滚动并启用 ViewPager 滚动
- clang-format - 使用 clang-tidy 输出格式问题
- python - For 循环检查表是否存在,如果不存在则在继续执行脚本之前创建它