4

This is my input:

<input [(ngModel)]="minimumRange" min="1" placeholder="0.0" step="0.1" type="number">

What I need is, when someone enters

"1"

, I need it to return

"1.0".

on blur How is this possible?

5
  • 1
    Use @Pipe. For example: stackoverflow.com/questions/38456114/… Commented Apr 5, 2017 at 7:57
  • @SrAxi, please post an example, thanks,. Commented Apr 5, 2017 at 8:01
  • is the type of minimumRange a string or a number ? Commented Apr 5, 2017 at 8:16
  • the type of minimumRange is : number Commented Apr 5, 2017 at 8:17
  • 1
    you need to output 1.0 because of style or because of some processing on the server or elsewhere that requires a number with 1 decimal ? Because the number representation of "1.0" is 1... Commented Apr 5, 2017 at 8:29

3 Answers 3

7

Using the number @Pipe you should be able to achieve this.

<input [ngModel]="minimumRange | number : '1.1-2'" min="1" (ngModelChange)="minimumRange=$event" placeholder="0.0" step="0.1" type="number">

For more info:

Hope it helped! Good coding bro!

Update:

If we use @Pipe in model like this:

<input [(ngModel)]="myModel| uppercase">

It will throw the following error:

Parser Error: Cannot have a pipe in an action expression at column X

We will just need to change it to this:

<input [ngModel]="myModel| uppercase" (ngModelChange)="myModel=$event">

Update2:

Added (ngModelChange)="minimumRange=$event" to keep the two way binding functionality.

As @n00dle pointed me, removing the () removes the 2 way binding functionality. So the way to use @Pipe in a 2-way-binding would be using also (ngModelChange).

This could be of huge use:

Sign up to request clarification or add additional context in comments.

16 Comments

I'm receiving template errors when I apply pipe mate.. that's why I asked.
pipes are not allowed in 2-way data-binding.
may i know where it is mentioned please pipes are not allowed in 2-way data-binding? @n00dl3
@n00dl3 Are allowed. Not allowed in action expression, but if we just change [(ngModel)] to [ngModel] it lets us apply the @Pipe.
@n00dl3 You did well pointing it out. My first answer wasn't correct nor precise, thanks! :)
|
2

try this

<input [(ngModel)]="minimumRange" min="1" placeholder="0.0" step="0.1" type="number" (keyup)='conversion()'>

conversion(){
  this.minimumRange = this.minimumRangex.toPrecision(2);
}

4 Comments

@n00dl3, please see the pipe comment. Thanks.
the issue with this is that I need to know how to put this in component. I can't place a script in HTML..
@Padeep Jain, Where does this.minimumRangex come from? Thanks
I believe it should be typo, there should be this.minimumRange
0

  private _minimumRange:number;

  get minimumRange():number{
    return this._minimumRange;
  }

  set minimumRange(num:number){
    this._minimumRange = num.toPrecision(2);
  }
      <input [(ngModel)]="minimumRange" min="1" placeholder="0.0" step="0.1" type="number">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.