Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2: why change event is not working on ion-select?

Tags:

angular

ionic2

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

like image 227
Sandeep Sharma Avatar asked Jan 16 '17 08:01

Sandeep Sharma


People also ask

How do I reset my ion select?

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.

How do you use Select in ionic?

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.

How do you get selected value from ION select?

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.

How do I set default value in ion select?

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.


3 Answers

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);
  }
like image 169
CodeMind Avatar answered Oct 13 '22 23:10

CodeMind


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.

like image 40
Suraj Rao Avatar answered Oct 13 '22 22:10

Suraj Rao


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);
}
like image 31
Arindam Avatar answered Oct 14 '22 00:10

Arindam