How to set the value Ionic 2 input field

I have a tiny question.

I am using this HTML code:

<ion-input id="amount" type="number" ></ion-input>

<button (click)="setinput()">Set Value</button>

And this TS code:

setinput(){
var input= document.getElementById("amount") as HTMLInputElement;
input.value=42;
console.log("amount");
console.log(input.value);
}

My console tells me that the number has been changed. But the updated value will not appear in my input field. Do you have any suggestions?

Thank Advance

l Trembon

+4
source share
1 answer

You can do it as follows:

page.html

<ion-input id="amount" type="number" [(ngModel)]="value"></ion-input>
<button (click)="setInput()">Set Value</button>

page.ts

export class Page {
  value: number = 0; // Default is 0

  constructor() {}

  setInput() {
    this.value = 42;
  }
}

[(ngModel)]="value", value . , . , " ". , .

, , Angular 2, . Angular , - .

+14

Source: https://habr.com/ru/post/1660328/


All Articles