Alaa Almishlab
Alaa Almishlab

Reputation: 1

ion-toggle checked doesn't wok with ngModel ionic3

Using [(ngModel)] with an ion-toggle component doesn't visually checked the component at the start if the bound value is true. [(ngModel)] attribute works fine but the component is not visually updated at load regarding its value.

Not checked at component load if [(ngModel)] is there

<ion-toggle checked="true" [(ngModel)]="n" (ionChange)="themestate(n)"></ion-toggle>

the output will be like

the output will be like

<ion-toggle checked="true" (ionChange)="themestate()"></ion-toggle>

when I remove ngModel its work like this

when I remove ngModel its work like this

Upvotes: 0

Views: 4636

Answers (2)

brijmcq
brijmcq

Reputation: 3418

There's still an open issue regarding this.

In Ionic v5, my workaround is to wrap the variable in a setTimeout

My code is something like this

// template
     <ion-toggle [(ngModel)]="foo"></ion-toggle>

// component
     foo: boolean;
  ngOnInit() {
     setTimeout(() => {
      this.unsettled = true;
      this.paid = true;
     }, 0);
   }



Upvotes: 0

Abinesh Joyel
Abinesh Joyel

Reputation: 2093

set n=true and assign to ngModel it will select it. I can't figure out what exactly you need

<ion-toggle [(ngModel)]="n"></ion-toggle>

Upvotes: 2

Related Questions