Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle click function on ion-toggle

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");
}
like image 951
Mohan Gopi Avatar asked Mar 31 '17 05:03

Mohan Gopi


People also ask

How do you toggle with ions?

Toggles are switches that change the state of a single option. They can be switched on or off by pressing or swiping them. Toggles can also be checked programmatically by setting the checked property.

How can I place text inside an ion toggle?

Slap ion-toggle-text on any existing ion-toggle and you're good to go. On/off text can be set with either the ng-true-value / ng-false-value attributes or with a ; separated value with the ion-toggle-text attribute. If no text is provided it defaults to "on" & "off".

Can't bind to ngModel since it isn't a known property of ion toggle?

To fix Can't bind to 'ngModel' since it isn't a known property of 'input' error in Angular applications we have to import FormModule in app. module. ts file. If you are using FormBuilder class to create reactive form we have to import ReactiveFormsModule as well to avoid below error.

How do I reduce the size of my toggle button?

The different Toggle Switch Button sizes available are default and small. To reduce the size of default Toggle Switch Button to small, set the CssClass property to e-small .


1 Answers

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/

like image 127
Günter Zöchbauer Avatar answered Sep 20 '22 21:09

Günter Zöchbauer