Ionic: Clear text field on page load

Viewed 492

i have a text field in my html page as below :

<ion-input type="text"   (input)="getid($event.target.value)" autofocus="true" id="get_ticket_id"></ion-input>

i want this text field to be sent to blank/empty every time page loads/ user comes to the page by opening application or using back-button

how can i achieve that, i have done lot of search on this but could not find the solution, can someone please advise me how can it be acheieved

1 Answers

Use ngModel, that is one of absolute essentials you should learn about Angular. Also together with ionViewWillEnter() as suggested. So something like this:

ts:

myInput = "";

ionViewWillEnter() {
  this.myInput = "";
}

getId(val) {
  console.log(val)
}

And the template:

<ion-input [ngModel]="myInput" (ngModelChange)="getId($event)"></ion-input>

So I also suggest to use ngModelChange instead of (input), we are coding with angular :)

Related