how to handle click function on ion-toggle

Mohan Gopi picture Mohan Gopi · Mar 31, 2017 · Viewed 13.5k times · Source

here is my .html file

<ion-item no-lines (click)="update()" >
        <ion-label> Notification</ion-label>
        <ion-toggle [(ngModel)]="notify" ></ion-toggle>
    </ion-item>

the above code works when i click on the Notification text but when i click on the ion-toggle i am not able to invoke the function what should i do to invoke the function.

here is my .ts file

update(){
 console.log("invoking notification");
}

Answer

G&#252;nter Z&#246;chbauer picture Günter Zöchbauer · Mar 31, 2017

If you use ngModel it's best to use ngModelChange

<ion-toggle [(ngModel)]="notify" (ngModelChange)="update($event)" ></ion-toggle>

otherwise you can use

<ion-toggle (ionChange)="update($event)"></ion-toggle>

See also https://ionicframework.com/docs/api/components/toggle/Toggle/