angular - How to format input text value to currency on anguar 4 on lost focus? -


i want format value of input text currency after losing focus. how can achieve in angular4?

<input type="text" name="txtamount" [(ngmodel)]="amount" /> 

any suggestion appreciated. thanks!

use currencypipe transform on (blur).

 <input type="text" name="txtamount" [(ngmodel)]="amount"          (blur)="transformamount($event)" [(value)]="formattedamount"/>   <br/>  <br/>  <div>     formatted amount on loosing focus: {{ formattedamount }}  </div>  <br/>  <div>      formatted amount inline pipe: {{ amount | currency:'usd'  }}  </div> 

and in .ts code,

transformamount(element: htmlelement){     this.formattedamount = this.currencypipe.transform(this.amount, 'usd');     // remove or comment line if dont want      // show formatted amount in textbox.     element.target.value = this.formattedamount; } 

you need import currencypipe '@angular/common' , add in app providers: [ ... ].


see plunker demo


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -