How to use currency pipe in Angular ReactiveForm Input

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 พ.ย. 2024

ความคิดเห็น • 47

  • @SaschaKleiber
    @SaschaKleiber 3 ปีที่แล้ว +2

    Question, maybe i missed it: Its nice you can format the view like that, but what about the actual value in the component? Will the form submit a string now cause of the formatting? I'm having the problem that I want the user just to see the formatted value in the form, but if he submits it of course I just want to submit the actual number, not a string with currency symbols etc. ...

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว +4

      Yes, you need to convert that string value to number in your onSubmit() function. You can use Object.assign(o, form.value) to make a copy of your form.value, and then write some code to change o, so strings become numbers (regular expression + Number() or parseInt() can handle that easily). You need to make a copy because you do not want to mess up your display on front end.

  • @bolajiobafemi3244
    @bolajiobafemi3244 3 ปีที่แล้ว +1

    Amazing solution was very helpful

  • @garrettwitzenburg2873
    @garrettwitzenburg2873 3 ปีที่แล้ว

    Thanks! Any stackblitz?

  • @prabuk3819
    @prabuk3819 ปีที่แล้ว

    In Reactive Form Controls Can't we use pipe in Html?

  • @mohsenmusavi3603
    @mohsenmusavi3603 2 ปีที่แล้ว

    very useful. Tnx

  • @prabuk3819
    @prabuk3819 ปีที่แล้ว

    By this approach, the form control value is changed as string with comma separated, but we need to save as number. Is that possible?

    • @turingcs2001
      @turingcs2001  ปีที่แล้ว

      You have to write code to parse string to number

    • @prabuk3819
      @prabuk3819 ปีที่แล้ว

      @@turingcs2001 Actually, If we have precisions like 123.4567, and need to show as 123.457% with roundoff three precision and percentage symbol, by this approach the form control value is getting changed as '123.457%'.

  • @damico750
    @damico750 2 ปีที่แล้ว +1

    It all worked out, thank you! But if you output the data to the html page {{ this.myForm.value | json }}, you'll see that the data changes in the model too. How can I make the data change only in the input?

    • @turingcs2001
      @turingcs2001  2 ปีที่แล้ว

      I have not yet figured out a way to do that.

  • @ThePridhvi
    @ThePridhvi 3 ปีที่แล้ว

    Thanks a lot for this solution!

  • @joeyvico
    @joeyvico ปีที่แล้ว

    One great solution, thanks

  • @shantanugupta4932
    @shantanugupta4932 3 ปีที่แล้ว

    The specified value "$1" cannot be parsed, or is out of range please help me , I am getting this error

  • @Antonov392
    @Antonov392 ปีที่แล้ว

    Nice!

  • @mancmanomomyst
    @mancmanomomyst 2 ปีที่แล้ว +3

    This is good but it doesn't work if your input is of type number.
    You can do this to get rid of the currency symbol:
    this.currencyPipe.transform(
    form.fieldName,
    'GBP',
    '',
    '0.2-2'
    );
    or you could also just use the DecimalPipe the same way but the number won't display if you have thousand separators.
    you could change it to string and do the conversion on save to strip then convert to a number again but you lose the browsers number support e.g. mousewheel or up down arrows to change value. Probably not an issue for your scenario but it could be in other cases

  • @viniciosbarbosa864
    @viniciosbarbosa864 5 หลายเดือนก่อน

    thank u so much

  • @plsreleasethekraken
    @plsreleasethekraken ปีที่แล้ว

    Using a directive is cleaner.

    • @mosouley
      @mosouley 10 หลายเดือนก่อน

      can you elaborate more on this approach?

  • @chikamiracle2798
    @chikamiracle2798 3 ปีที่แล้ว

    Helpful, but please how do i use currency pipe in Angular Template Driven Form Input

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว +1

      I never used template driven form. I prefer new technology.

  • @kabezzaa
    @kabezzaa 3 ปีที่แล้ว

    ty very much!

  • @bharathparsam
    @bharathparsam 4 ปีที่แล้ว

    Hello wang,
    This solution is amazing , but I am facing issue when I try to enter a value like 12345.67 ..
    It’s not accepting the ‘. ‘ decimal when entered manually but if I enter decimal point after entering the whole number , it is accepting

    • @turingcs2001
      @turingcs2001  4 ปีที่แล้ว

      The example I supplied only works on whole dollars, not cents. Under section 'use pipe to display currency' (1:33), I wrote custom code (using regular expression) to remove 1. all leading zeros 2. anything non-digit. That's why '.' got removed. You need to modify my code to allow '.', and also change the currency pipe specification ('1.0-0' is for whole dollars only).

  • @kanimozhi1416
    @kanimozhi1416 3 ปีที่แล้ว

    Hi, any option to display dollar amount at right side of excel using Currency pipe

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว

      This is Angular, not Excel. If you want to display $ on the right hand-side, you probably need to make your own pipe because I doubt the built-in currency pipe has dollar displayed on the right setup.

  • @sanjivasen
    @sanjivasen ปีที่แล้ว

    Hi how to fomar canadian French fformat

  • @isrozuna1586
    @isrozuna1586 3 ปีที่แล้ว

    Good video! Very helpfull!
    But, I have a question . If I need decimals? Is there any way to put it? for Example $1,245,984.86
    Thanks!

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว +1

      Change pipe parameter from 1.1-0

    • @isrozuna1586
      @isrozuna1586 3 ปีที่แล้ว

      ​@@turingcs2001 I did it, but for some reason the input works, but now it lets me enter any character , I did it this
      formattedAmount: this.currencyPipe.transform(form.formattedAmount.replace(/\D/g, '').replace(/^0+/, ''), 'USD', 'symbol', '1.1-0') and I get the follow error in console
      core.js:5980 ERROR Error: InvalidPipeArgument: 'The minimum number of digits after fraction (1) is higher than the maximum (0).' for pipe 'CurrencyPipe'

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว +1

      @@isrozuna1586 you need to read currency pipe documentation to find out the correct argument. Try removing the 1.1-0 argument. I believe the default format shows cents.

  • @mahdyataey1051
    @mahdyataey1051 3 ปีที่แล้ว

    hello dear friend! thank you for your tutorial. but I need this for controls I mean for form details. not for the main form I want to do this for details of a main form.

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว

      I will create a new video tonight. Sorry didn't follow replies--work kept me busy.

  • @diegosantillan1222
    @diegosantillan1222 3 ปีที่แล้ว

    How convert to number for the save later? Please

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว +1

      Use regular expression .replace(/\D/, ‘’) to remove non-digits and parseInt() to convert to number. JavaScript 101

  • @Tobawa2601
    @Tobawa2601 3 ปีที่แล้ว +1

    Well it does format the values correctly, but also puts the input-cursor always to the end of the value, which makes modifying values in the middle and front exhausting. Is there a fix for that?

    • @sk0vgaard
      @sk0vgaard 3 ปีที่แล้ว

      agreed

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว

      You can subscribe to onBlur event, so nothing happens until the user mouses away

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว

      Also, moving cursor to the end is the right default choice. If you move cursor to the middle or front by default, people will find it exhausting when they want to edit the end.

  • @STRIKEtetra
    @STRIKEtetra 4 ปีที่แล้ว

    Prefiro criar um pipe compartilhado e usá-lo em qualquer outro componente a fim de reutilizar código e deixar o projeto mais limpo e com bundles menores.

  • @ChrisSpielt
    @ChrisSpielt 3 ปีที่แล้ว +2

    What if you want a "." instead of ","?

    • @mbonet
      @mbonet 2 ปีที่แล้ว

      I guess your question is related to localization. You can use the getLocaleNumberSymbol function in order to get **your current locale's** grouping and decimal characters and then adjust the regular expression with this characters (instead of hardcoding , and .

  • @actsofkindness4839
    @actsofkindness4839 3 ปีที่แล้ว

    form.newWorth.replace error replace is not a function

    • @turingcs2001
      @turingcs2001  3 ปีที่แล้ว

      replace() is a function on a string. your newWorth is probably a number so it doesn't have a replace function.