12

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");
}
Mohan Gopi
  • 7,606
  • 17
  • 66
  • 117

1 Answers1

29

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/

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    is there any other way i can use because ionchange method invokes two time. when ever my page appears i am setting my toggle to be true or false because of that i am automaticaly invoking the function but i want when the user manually choose that time only i shuodl invoke the function – Mohan Gopi Mar 31 '17 at 06:02
  • 1
    What about `ngModelChange`? Does it also call twice? Otherwise you might need to ignore the first event. I don't think there is any other way. – Günter Zöchbauer Mar 31 '17 at 06:04