ionic-framework: bug: Input type number is not respected

Type: <span ionic-type>bug</span>

Ionic Version: <span ionic-version>2.x</span>

Platform: <span ionic-platform>all</span>

<span ionic-description>Steps to reproduce:

  1. Create an ion-input with type="number"
  2. Add [(ngModel)] and (ngModelChange) to get the Value of the input
  3. Check the type of the value using typeOf(value)
  4. Type in a number into the input => Type is always a string

Expected behavior: … => Type is a number.

See this plunk as an example: https://run.plnkr.co/plunks/GHBFad/</span>

<span is-issue-template></span>

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 8
  • Comments: 28 (12 by maintainers)

Most upvoted comments

I confirmed this is still an issue with latest.

workaround:

Template:

 <ion-input 
    type="number" 
    [ngModel]="progress" 
    (ngModelChange)="progress = convertToNumber($event)">
  </ion-input>

Function:

public convertToNumber(event):number {  return +event; }

hope this helps.

@n2lose For me the workaround by @dpsoft works. I’m still hoping for a proper fix though.

I think there are a few issues going on in this one issue. Here’s my attempt at a recap (let me know if anything I said is wrong):

  1. Original issue by @flavordaaave - using <ion-input type="number"> the value is returning string type instead of number. I can’t get the original plunker to work currently, thanks @mirkonasato for the plunkers, can reproduce with: http://plnkr.co/edit/EjsP3h?p=preview
  2. @awe-sim - Alert is only accepting / returning string values in inputs
  3. @marpstar / @joshgarwood - numeric keyboard is not appearing with <ion-input type="number">

So our issue is that we pass input type dynamically here: https://github.com/ionic-team/ionic/blob/master/src/components/input/input.ts#L103

Which is broken and reported by this Angular issue: https://github.com/angular/angular/issues/13243

Reopening this since it works without the dynamic binding.

Note to team - it may be blocked by Angular but it’s worth investigating. Maybe we should separate the alert / keyboard issue out.

The underlying issue is with setting the type dynamically, in input.ts.

That doesn’t work with plain Angular either, the value becomes a string after editing it: http://plnkr.co/edit/mG5F636NLpkmj09UPWkd?p=preview

Angular’s NumberValueAccessor directive has input[type=number] as its selector. I guess that’s only evaluated when the component is created, and at that point the type is not yet set?

@shootdaj Good catch with the emitEvent: false. That is all correct. Basically, this needs to be fixed and there is no good, scalable workaround. This should really be higher on ionic’s priority list

Seems we still need to find some workaround to fix it temporary

Any progress on this topic?

The original plunk doesn’t seem to work any more.

Here’s a new one with Ionic 2.0.0-rc.3: http://plnkr.co/edit/EjsP3h?p=preview

Note that this works as expected with plain Angular: http://plnkr.co/edit/rIr9jVkUnIfW1FZIbROx?p=preview