Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Toggle call ionChange on view init #8070

Closed
MatanYadaev opened this issue Sep 13, 2016 · 16 comments
Closed

bug: Toggle call ionChange on view init #8070

MatanYadaev opened this issue Sep 13, 2016 · 16 comments

Comments

@MatanYadaev
Copy link

Type: bug

Ionic Version: 2.x

Platform: all

This is my toggle:
<ion-toggle [(ngModel)]="workMode" (ionChange)="workModeChanged()">

if this.workMode == true so workModeChanged callback will called on page init, even without click on the toggle. that is the bug.
if this.workMode == false there is no bug.

Thanks

@jgw96
Copy link
Contributor

jgw96 commented Sep 15, 2016

Hello! Thanks for opening an issue with us! As this seems like more of a support question i will urge that you ask this question on our forum. Thanks for using Ionic!

@jgw96 jgw96 closed this as completed Sep 15, 2016
@MatanYadaev
Copy link
Author

@jgw96 That is not abouy support its a bug...

@jgw96
Copy link
Contributor

jgw96 commented Sep 15, 2016

Hello @MatanYed sorry for the misunderstanding! Would you mind sharing a code sample that i can use to reproduce this issue? Thanks!

@MatanYadaev
Copy link
Author

@jgw96 i am sorry i am from the phone so i cant put here code
But just create 2 pages
In the second page put ion toggle with ng model and ion change event
When you enter the second page and ngModel is true, ionChange event will fire
It shouldnt fire because the page only initialized

@jgw96
Copy link
Contributor

jgw96 commented Sep 15, 2016

No problem, thanks! Are you initializing the value ngModel is bound too on the second page?

@jgw96 jgw96 reopened this Sep 15, 2016
@lorenzob-incomedia
Copy link

Hi,
same issue for me. ionChange is fired on init if the ngModel evaluation is true.
In my case for the toggle component there is two-way data binding like that:

<ion-toggle primary [(ngModel)]="currentSite.usersNotifications" (ionChange)="preferenceChanged()"></ion-toggle>

The model currentSite is initialized in the constructor using a parameter of the view like that:

private currentSite: SiteModel = null;
constructor(public nav: NavController, public navParams: NavParams) { this.currentSite = navParams.get('site'); }

Hope this help.
Thanks!

@manucorporat
Copy link
Contributor

manucorporat commented Sep 30, 2016

I can't reproduce this issue, in fact it was already fixed a long time ago:
#6144

What version of ionic are you using?
Closing issue until additional info is provided. Maybe a repo?

@mcwienczek
Copy link

I can reproduce the issue again on Ionic 3. I have fetched the value from backend and set the field in both constructor and ngOnInit. Both ways cause event to fire.

Ionic version:

"ionic-angular": "3.0.1"

Constructor code:

constructor(

private navCtrl: NavController,
private popoverCtrl: PopoverController,
private userService: UserService,
private toastHelper: ToastHelper) {
  this.userService.getUser().subscribe(
  user => {
    this.disableAccount = user.disableAccount;
  },
  err => {
    this.toastHelper.showTranslatedToast('connection.serverUnavailable');
    console.log(err);
  }
);

}

HTML:

<ion-item>
<ion-label>Disable account</ion-label>
<ion-toggle [(ngModel)]="disableAccount" (ionChange)="toggleDisableAccount()" checked="false"></ion-toggle>
</ion-item>

@manucorporat
Copy link
Contributor

I can reproduce the issue again on Ionic 3. I have fetched the value from backend and set the field in both constructor and ngOnInit

but this is not a sync task... the value is changed before the component is initialized

@mcwienczek
Copy link

mcwienczek commented Apr 27, 2017

I have just refactored code so that I don't have async HTTP request anymore. It's just simple value assignment now.

ngOnInit() {
      this.user = this.userService.user;
  }

still fires the event though...

@manucorporat
Copy link
Contributor

@VSMax can you update to 3.1?

@mcwienczek
Copy link

mcwienczek commented Apr 27, 2017

Sure, I will let you know soon if the issue still exists.

@mcwienczek
Copy link

mcwienczek commented Apr 27, 2017

Upgraded to Ionic 3.1, my packages now:

"dependencies": {
    "@angular/common": "4.0.2",
    "@angular/compiler": "4.0.2",
    "@angular/compiler-cli": "4.0.2",
    "@angular/core": "4.0.2",
    "@angular/forms": "4.0.2",
    "@angular/http": "4.0.2",
    "@angular/platform-browser": "4.0.2",
    "@angular/platform-browser-dynamic": "4.0.2",
    "@ionic-native/core": "3.4.4",
    "@ionic-native/splash-screen": "3.4.4",
    "@ionic-native/status-bar": "3.4.4",
    "@ionic-native/app-version": "3.4.4",
    "@ionic-native/camera": "3.4.4",
    "@ionic-native/network": "3.4.4",
    "@ionic/storage": "2.0.1",
    "ionic-angular": "3.1.0",
    "ionicons": "3.0.0",
    "@angular/animations": "4.0.2",
    "@angular/platform-server": "4.0.2",
    "@ngx-translate/core": "^6.0.1",
    "@ngx-translate/http-loader": "0.0.3",
    "@types/cordova-plugin-globalization": "0.0.3",
    "decimal.js": "^7.1.1",
    "moment": "2.18.1",
    "rxjs": "5.3.0",
    "sockjs": "^0.3.15",
    "sockjs-client": "^1.1.1",
    "stompjs": "^2.3.3",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.5"
  },
  "devDependencies": {
    "@ionic/app-scripts": "1.3.4",
    "gulp": "^3.9.1",
    "typescript": "2.2.2"
  },

but the problem still remains

@mcwienczek
Copy link

After upgrading to 3.1.1 issue is fixed.

@austinemayer
Copy link

@mcwienczek I am now on Ionic 3.2.0 and this is still an issue for me.

@ionitron-bot
Copy link

ionitron-bot bot commented Sep 2, 2018

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 ionitron-bot bot locked and limited conversation to collaborators Sep 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants