Ionic 2: How to call a method when select value is changed

CMA picture CMA · Nov 16, 2016 · Viewed 35.8k times · Source

I am new to Ionic 2, I read the Ionic 2 Documentation over and thought this code would work. Its supposed to give back the current select value when it's changed and print it to console.

page.html

<ion-select #C ionChange="onChange(C.value)"> 
                    <ion-option value="a">A</ion-option>
                    <ion-option value="b">B</ion-option>
</ion-select>

page.ts

public CValue:String;
onChange(CValue) {
     console.log(CValue);
}

However the console isn't giving out anything related to this. Did I miss something in the binding?

Answer

sebaferreras picture sebaferreras · Nov 16, 2016

Instead of

<ion-select #C ionChange="onChange(C.value)"> 
  ...
</ion-select>

Since ionChange is an event (and not a simple attribute) you need to do it like this:

<ion-select #C (ionChange)="onChange(C.value)">
  ...
</ion-select>