Why change event is not working on ion-select?
I tried with following statement:
<ion-select formControlName="shippingMethod (change)="shippingMethodChange($event)">
I also tried with ionChange by using following and This is working:
<ion-select formControlName="shippingMethod (ionChange)="shippingMethodChange($event)">
It is being said "We can use Angular event bindings to respond to any DOM event" and here is the doc for the change event
https://developer.mozilla.org/en-US/docs/Web/Events/change
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.
A select should be used with child <ion-select-option> elements. If the child option is not given a value attribute then its text will be used as the value. If value is set on the <ion-select> , the selected option will be chosen based on that value.
We can access the Ionic select by using a standard <ion-select> element. A select component always used with child <ion-select-option> element. If the <ion-select-option> does not have a value attribute, then its text will be used as the value.
If you deal with default value xor objects, the cleanest way is to provide a compare function to the [compareWith] attribute. This function takes 2 objects in parameters and returns true if they are equals. This way, existing values in model will be selected at start.
You can use it the following way:
<ion-select [(ngModel)]="gender" (ionChange)="onSelectChange($event)">
<ion-option value="G">Colombo</ion-option>
<ion-option value="M">Galle</ion-option>
</ion-select>
then in your .ts file:
onSelectChange(selectedValue: any) {
console.log('Selected', selectedValue);
}
ion-select
is a custom component by Ionic 2 . Check api here.
It is built by ionic leveraging an alertController/actionsheetcontroller
based on developer requirement.
It does not use change event like an HTML select and the correct event emitted is ionChange
.
Edit
And the Event emitted for clicking on ion-option
is ionSelect
.
Check this github issue.
Component docs here
Hope this answers your question.
The question keeps popping up every now and then. According to the documentation ionSelect is an output event on option (ion-option)
Here is an example usage
<ion-select>
<ion-option *ngFor="let opt of options" [value] = "opt" (ionSelect)="triggerMe(opt)">{{opt}}</ion-option>
</ion-select>
And then
triggerMe(value: string): void {
console.log("selected value", value);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With