Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 902
CMA Avatar asked Nov 16 '16 10:11

CMA


1 Answers

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>
like image 188
sebaferreras Avatar answered Sep 20 '22 19:09

sebaferreras