How to reset Ion-Select form field in Ionic 3?

Stephen Romero picture Stephen Romero · Feb 7, 2019 · Viewed 7.1k times · Source

I want to present an option to allow user to clear the ion-select form field if they decided to not select an option after already selecting, but I'm having a hard time finding anything to help out. Perhaps something like this? Ionic doesn't have any resources for resetting.

<ion-option (ionChange)="resetValue()">Reset</ion-option>

Any help is appreciated.

Answer

Sudarshana Dayananda picture Sudarshana Dayananda · Feb 7, 2019

You cannot use ion-option to reset ion-select form field. But you can provide clear button to reset ion-select if an ion-option is selected as below.

HTML

<ion-content padding>

  <ion-list>
  <ion-item>
    <ion-label>PROJECT</ion-label>
    <ion-select [(ngModel)]="project">
      <ion-option *ngFor="let project of projects" value="{{project}}">{{project}}</ion-option>
    </ion-select>
  </ion-item>
</ion-list>

<button *ngIf="project" ion-button (click)="reset()">clear</button>

</ion-content>

TS

export class HomePage {

  projects: any = [];
  project: string;

  constructor() {
    this.projects = ["project 1", "project 2", "project 3", "project 4"];
  }

  reset() {
    this.project = null;
  }
}

Find working example here