-
Notifications
You must be signed in to change notification settings - Fork 85
ion-toggle unnecessarily triggers change
event when default value is true
#127
Comments
Still happening in Ionic 4. Only happens when the default value related with [(ngModel)] is true. However, the first time I go into the page where the element is, everything goes fine, but if I navigate to other screen and go back then the error occurs. I ended up here coming from the original thread in bountysource.com. I'm surprised that this has not been fixed after 3 years and 2 mayor versions.. I'll be happy to answer any questions to help solve this. This is part of my code:
component.html
If I set |
So, I managed to fix the problem with a workaround that is quite simple, so I wouldn't even call it a workaround. Instead of using Final html:
|
Still happening in Ionic 5. It has been open since like 3 years without an official solution... |
Would like to see this bug fixed in Ionic v5! |
I don't think that this will get fixed. The behavior is expected, I think. Instead of using actual checkboxes, I used buttons and images of checkboxes.
It's not perfect as it's not using the neat implementation of the in-house And I don't have to bother about |
Guys I have been into this annoying problem with ion-checkbox when ever I open a new page the the API loads the data and checks the item with 1 or 0 because [(ngModel)] was binding the state to the checkbox. I don't know if it's too late for explanation but I hope this helps someone. I am using Ionic V5 with Angular 11. There are few workaround for this given in Github and Ionic forum, the best I can find is If you are using the checkbox to call some API or do some asynchronous task which will change the bound value, then Don't do this : Here the directive would trigger the checked state of the checkbox when the page loads or list is refreshed which will trigger ionChange event again and again infinitely. Or this: Here you are already using the two way binding which is sending instruction to the controller and again you are calling the function with model change event so it will keep calling the function back and forth. Or even this: This might work for some but it's uncertain as in long lists it might take some time to render the complete lists and some items might get dirty a little late which will change the model and trigger the event when list is refreshed. The thing worked for me is: In this case the checked property will get value as soon as the list is initialized and check the checkboxes but it wouldn't trigger ionChange event until manually checked. But if you want to change the bound value which is
and you can add an extra layer of security by comparing if the item's value and checked state is matched or not inside the function in case any leaked changed detection happens. Complete solution:
I hope this helps in other Ionic input components as well. |
For anyone who is still being trapped in this issue, I have found a workaround: <IonSelect
okText="OK"
cancelText="CANCEL"
slot="end"
value={status}
// Key property prevents the first trigger.
key={status}
onIonChange={async (event) => {
const value = event.detail.value;
// The following prevents the second trigger.
if (value === status) return;
// Do something...
}}
> |
Original issue by @cubicleWar on 2016-08-20T22:45:06Z
This is reposting of issue #6144 from beta 4 it appears to have made its way back into beta 11
Short description of the problem:
ion-toggle unnecessarily triggers change event when the default value is true
What behavior are you expecting?
The change event should not fire until the user presses the toggle
Steps to reproduce:
toggle: boolean = true;
<ion-toggle [(ngModel)]="toggle" (ionChange)="onUpdateToggle()">
Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc)
Which Ionic Version? 1.x or 2.x
2.0.0-beta.11
Plunker that shows an example of your issue
Note this is the plunk of the original issue there seems to be no plunk template for beta 11 and this is not an issue in beta 10. I have attached a sample project to demonstrate the problem in beta 11
http://plnkr.co/edit/f2NSVLP1DETbkyFmpSN8?p=preview
Run ionic info from terminal/cmd prompt: (paste output below)
Cordova CLI: 6.3.1
Gulp version: CLI version 3.9.1
Gulp local: Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.11
Ionic CLI Version: 2.0.0
Ionic App Lib Version: 2.0.0
ios-deploy version: 1.8.6
ios-sim version: 5.0.8
OS: Mac OS X El Capitan
Node Version: v4.4.5
Xcode version: Xcode 7.3 Build version 7D175
The text was updated successfully, but these errors were encountered: