Calliste Hanriat Calliste Hanriat - 1 month ago 17
TypeScript Question

Get value from input number Angular 2

I'm trying to recover a value from an input with ionic 2 and angular 2.
I want a value of type number.

My HTML :

<ion-item>
<ion-label floating>Number</ion-label>
<ion-input id = "choosenNumber" type="number" value=4 ></ion-input>
</ion-item>


My TypeScript :

var input_element = <HTMLInputElement> document.getElementById("choosenNumber");
var choosenNumberValue = input_element.valueAsNumber; //does not work !! (valueAsNumber undefined)


Any ideas ?

Answer

Okay I've found the solution by using [(ngModel)]. Thanks

Here is the solution : [HTML]

<ion-item>
  <ion-label floating>Number</ion-label>
  <ion-input type="number"  [(ngModel)]="chVal" name = "chVal" value=4 ></ion-input>
</ion-item>

[TS]

export class MyClass{
   chVal:number;

chVal is the good value !!

Comments