The change event does not fire when I change a boolean value from false to true after page load. The event fires if the change is from true to false. Any suggestions? Below is the code to replicate and environment info. Here is a plunker that shows the behavior. On the first toggle only one change event fires. But subsequent toggles fire two change events.
Great catch. I messed up the function name in the .ts file. I corrected it now but still, this is all I get in the console: âFired toggleToFalse()â.
Here is the updated .ts file:
public toggleBoth(){
this.booleanFlag1 = false;
this.booleanFlag2 = true;
}
public toggleToFalse(){
console.log(âFired toggleToFalse()â);
}
public toggleToTrue(){
console.log(âFired toggleToTrue()â);
}
Keep in mind this is just a basic example to demonstrate the problem I am having which is the event not firing when the boolean is changed from false to true. In this case when booleanFlag2 is updated from the initial value of false to true inside the toggleBoth() method. I am expecting the toggleToTrue() to fire but it does not. Hope this clarifies.
I did not understand the problem quite well, but Iâll try.
The first ion-toggle you trigger a function that doesnât ever change. So if you executed once, youâll get that this.booleanFlag1 = false; this.booleanFlag2 = true;
If you execute the function again, youâll have the exact same lines of code, leaving this.booleanFlag1 = false; this.booleanFlag2 = true;
If you tap the second ion-toggle, you will notice that you are not changing itâs value, so itâll remain the same. It will console.log () though.
The idea here is to perform the toggle only once on the first toggle button which calls the toggleBoth() method. This will change the value of the both boolean variables. booleanFlag1 will go from true to false and booleanFlag2 will go from false to true. Now we observe the change events firing by watching the console.log output. But in my case only the toggleToFalse fires. Hope this helps. Hugo was kind enough to try it but he did not experience the problem so I am suspecting I have a version issue to resolve.
Hello Raul, I created a plunker that hopefully helps show the problem more clearly. The first time you toggle the top button, only one change event fires. But subsequent toggles will fire two change events. You can find the plunker here and thank you for your interest in helping our: http://plnkr.co/edit/r3WsxzWDR0ssko4shkda?p=info
Now I get it. The names were very confusing for me. I understand the problem now and find it very annoying. I canât find a way of making it work, besides:
I managed to fix the problem by simply adding [checked]=âpreference.enabledâ to the ion-toggle element. I found some other examples on-line, decided to try it and the problem went away. Not sure I fully understand why but things are working ok now. Thanks again for your interest in helping out.