ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

ion-toggle unnecessarily triggers `change` event when default value is true

Open wli opened this issue 10 years ago • 8 comments

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:

  1. Create an ion-toggle with ngModel bound to a variable with a default value of true
  2. Observe change event firing without any user input
toggle: boolean = true;

<ion-toggle [(ngModel)]="toggle"
                      (change)="onUpdateToggle($event)"></ion-toggle>

Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc) This only fires when the default value is true.

Which Ionic Version? 1.x or 2.x 2.0.0-beta.4

Plunker that shows an example of your issue

http://plnkr.co/edit/f2NSVLP1DETbkyFmpSN8?p=preview

Run ionic info from terminal/cmd prompt: (paste output below) Cordova CLI: 6.0.0 Ionic Framework Version: 2.0.0-beta.4 Ionic CLI Version: 2.0.0-beta.24 Ionic App Lib Version: 2.0.0-beta.14 OS: Distributor ID: Ubuntu Description: Ubuntu 15.10 Node Version: v5.8.0

wli avatar Apr 12 '16 23:04 wli

I can confirm this.

danielcrk-cn avatar Apr 13 '16 15:04 danielcrk-cn

It seems like this issue is back as of 2.0.0-beta.11

cubicleWar avatar Aug 20 '16 05:08 cubicleWar

I can reproduce on RC.0 as well

jvelo avatar Oct 08 '16 16:10 jvelo

Any update on this? In my case ion-toggle, ionChange runs on initial ngFor if value is set to true. I dont want that. Has anyone found a solution?

asamuga avatar Nov 14 '16 18:11 asamuga

Well, my fix is set the change function to fire only if I set a boolean value to true, while setting it to false on the event itself and the component constructor. Then, put one tap event function to set it to true, magic 💃, but not ideal.

export class MyComponent {
    constructor() {
       this.enableCallback = false;
    }
    functionCalledOnChange() {
         if (this.enableCallback) {
             //Your logic *here*
            this.enableCallback = false;
        }
    }
    functionCalledOnTap() {
         this.enableCallback = true;
    }
 }

ErvalhouS avatar Jan 29 '17 16:01 ErvalhouS

The issue is still exists! I'm using ionic 2.4.8

mizdler avatar Apr 09 '17 17:04 mizdler

I am now on Ionic 3.2.0 and this is still an issue. Has anyone been able to find fix for this?

austinemayer avatar Aug 03 '17 14:08 austinemayer

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Sep 02 '18 08:09 ionitron-bot[bot]