0

I have a number property that the user can edit in an input field. The number is bound to the input with [(ngModel)]=<value>

The input only shows the necessary fraction digits but I want it to show always at least 2 fraction digits. e.g. the number is 12.1 is displayed as '12.1' but i want it to be 12.10 just as if i would use the numberpipe as following <value> | number '.2'. Since it is a two-way binding angular disallows the use of pipes.

What I expect:

  • 12 ---> 12.00
  • 12.1 ---> 12.10
  • 12.12 ---> 12.12
  • 12.123 ---> 12.123
  • null ---> empty
Jonas Marty
  • 348
  • 3
  • 13
  • 1
    You can achieve it by making a custom pipe! – Sunny Parekh Jul 22 '19 at 10:02
  • so you want to change the value as user types it or after user stops typing and input loses focus? – yashpatelyk Jul 22 '19 at 10:14
  • @yashpatelyk the value should be present in the custom format after the value is first displayed. The format should not be applied while the user is typing as this would interrupt the user but formatting on losing focus would be nice. – Jonas Marty Jul 22 '19 at 11:32
  • @SunnyParekh I can't use pipes within the `[()]` syntax. Du you mean directives? – Jonas Marty Jul 22 '19 at 11:36
  • @JonasMarty, you can find your answer here: https://stackoverflow.com/questions/39642882/using-pipes-within-ngmodel-on-input-elements-in-angular – Sunny Parekh Jul 22 '19 at 12:14

0 Answers0