我有一个 HTML 输入:
<input [(ngModel)]="item.value" name="inputField" type="text" />
我想格式化它的值并使用现有的管道:
.... [(ngModel)]="item.value | currency:'USD':true" .....
此外,我正在尝试按以下方式使用它,但它第一次为我提供了理想的输出,并在更新字段时显示错误:
<input type="text"
[ngModel]="item.value | currency:'USD':true"
(ngModelChange)="item.value=($event)">
上面的代码会导致下面的错误。
ERROR Error: InvalidPipeArgument: '' for pipe 'CurrencyPipe'
at invalidPipeArgumentError (common.es5.js:2610)
at formatNumber (common.es5.js:3176)
at CurrencyPipe.webpackJsonp.../../../common/@angular/common.es5.js.CurrencyPipe.transform (common.es5.js:3350)
at LandingPageComponent.webpackJsonp.../../../../../src/app/guest-handling/landing-page/landing-page.component.ts.LandingPageComponent.transformAmount (landing-page.component.ts:54)
at Object.eval [as handleEvent] (LandingPageComponent.html:38)
at handleEvent (core.es5.js:12014)
at callWithDebugContext (core.es5.js:13475)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:13063)
at dispatchEvent (core.es5.js:8607)
at core.es5.js:9218
最佳答案
以下是货币管道工作得很好的方法:
<input
matInput
type="text"
placeholder="Test Price"
[ngModel]="testPrice | currency:'USD':'symbol':'2.2'"
[ngModelOptions]="{updateOn:'blur'}"
(ngModelChange)="testPrice=$event"
/>
基本上使用 ngModelOptions 更新模糊允许在输入字段中键入时不添加 0。
关于Angular 4 - 如何在输入类型中使用货币管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48321610/