ngModel does not change Ionic 2’s Toggle

Asked

Viewed 64 times

0

I have a toggle that enables and disables a function, it already works, what does not work is the state of Toggle in the View, toggle is always activated even with ngmodel=false. code below:

View:

<ion-toggle [(ngModel)]="Ativar" (ionChange)="SimNao()"></ion-toggle>

TS:

SimNao(){
    if(this.Ativar == true){
      this.storage.set('ativar', true);
    }
    if(this.Ativar == false){
      this.storage.set('ativar', false);
    }
  }

Console

console.log(this.Ativar);

Both on the console and on-premise, the values are alternated between True and False and on Local Storage everything is working, the function updates the data normally. The only problem is that toggle is always enabled even if the state is false.

  • Activar is a get Activar() ?

  • I wasn’t picking up at any point the value of this.Activate in the Path location, so the error persisted. But I was able to resolve.

1 answer

0


I was able to solve by taking the value of the toggle that was stored in the Localstorage, in this code I didn’t get the value of Toggle, so the solution was to create within the constructor the code below.

this.storage.get('ativar').then((data) => {
    this.Ativar = data;
});

This way I pass the correct state of the condition stored in the Bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.