Currency Format in an input of Angular2

1

When we want to format a tag in Angular2 as a currency we use the Pipe Currency:

<p>{{cantidad | currency}}</p>

That works perfectly, but if what we want is for the user to put an amount of money in an input, how do we get it to be formatted correctly?

What I would like is something like this (this example does NOT work, of course):

<input [(ngModel)]="cantidad | currency"></ion-input>

And the expected behavior would be that even if the user types "10.2", when the input loses the focus in the input it will see "€ 10.20" and in the model we will have "10.2".

Note: I know that I could separate [ngModel]="quantity" on the one hand and handle the event (change) or the (ngModelChange) on the other hand, but I was looking for a more elegant and comfortable solution.

Thank you very much

    
asked by Carlos Adrián 05.12.2016 в 21:37
source

1 answer

0

Better do a validation that verifies that it is of the type currency or what contains two decimals before saving it to avoid problems when converting it. Otherwise you will have to change the format in the logic of the component and add the decimals if they pass you a whole number.

    
answered by 11.04.2017 в 23:18