Angular 4 - 如何在输入类型中使用货币管道

标签 angular html-input angular-pipe angular2-ngmodel

我有一个 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/

相关文章:

angular - 如何读取 Angular 2 中组件输入属性绑定(bind)的字符串值?

angular - 单击按钮时, Angular 4 中的 react 形式提交复选框值数组

javascript - 以编程方式更新 Webshim 日期在 IE 8 中不起作用

angular - 禁用 Angular 默认键值管道排序

Angular 2% 管道输出不正确

css - 如何通过CSS设置无效p-inputNumber PrimeNg表单组件的红色边框?

css - 如何自定义 Angular Material Input 中的样式?

javascript - 输入值将保存在哪里?

android - 文件输入 - 在 android 上接受来自相机或图库的图像

Angular 管道 - 当选择其他选项管道过滤器时重置复选框