Angular 4 - How to use currency pipe in input type

后端 未结 2 2051
庸人自扰
庸人自扰 2021-02-06 01:43

I have an HTML input:


I want to format its value and use an existi

相关标签:
2条回答
  • Here is what worked just fine with currency pipe:

    <input
      matInput 
      type="text"
      placeholder="Test Price"
      [ngModel]="testPrice | currency:'USD':'symbol':'2.2'"
      [ngModelOptions]="{updateOn:'blur'}"
      (ngModelChange)="testPrice=$event"
    />
    

    Basically using the ngModelOptions to update on blur allows for the 0s not to be added while typing in the input field.

    0 讨论(0)
  • 2021-02-06 01:59

    I think this is a solution for you:

    <input type="text" 
       [ngModel]="item.value | currency:'USD':true" 
       (ngModelChange)="item.value=currencyInputChanged($event)">
    

    And then in your controller. Currency mark in from value in input:

      currencyInputChanged(value) {
        var num = value.replace(/[$,]/g, "");
        return Number(num);
      }
    
    0 讨论(0)
提交回复
热议问题