Reputation: 3022
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.
<ion-item class="formField ionField">
<ion-label color="primary" stacked>PROJECT</ion-label>
<ion-select
#projectName
ngModel
name="project"
interface="action-sheet">
<ion-option (ionSelect)="projectSelect(project.ProjectName,i)" [value]={ID:project.ID,Name:project.ProjectName} *ngFor="let project of projectArray; let i = index" >{{project.ProjectName}}</ion-option>
</ion-select>
</ion-item>
<ion-option (ionChange)="resetValue()">Reset</ion-option>
Any help is appreciated.
Upvotes: 3
Views: 10614
Reputation: 1
Another option that I figured out is to use two exaclty identical ion-select in parallel with their rendering controlled by ngIf. If the ngModel has value then first ion-select gets rendered, otherwise, second one gets rendered and because while rendering a new ion-select, it will not have any selected option by default, it works perfectly fine for both (null and non-null) ngModel values.
Upvotes: 0
Reputation: 5265
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
Upvotes: 5