I have my ion-toggle defined and I have set 2 properties to it as below:
<ion-toggle [checked]="storageStatus" (ionChange)="updateItem()"></ion-toggle>
everything is working just fine, I am able to toggle and change values to the localStorage and so on. But I am not sure why toggle button falls back to off state (or false state). when I move to another page and comeback.
Only Toggle button shown "visually" falls to Off state and not the values. I want button to be in the state in which I have set in localStorage while toggling.
The toggle value is correct and so binding value too.
storageStatus: any;
updateItem(){
this.storageStatus = !this.storageStatus;
window.localStorage.setItem('item', this.storageStatus);
}
Entire Code - update
Below Solution works for me now. I am not sure if its a good approach or not.
app.component.ts (when app loads setItem to true)
ngOnInit() {
window.localStorage.setItem('item', 'true');
}
typescript file
import { Component} from '@angular/core';
import { NavController, NavParams, ToastController } from 'ionic-angular';
@Component({
selector: 'page-settings',
templateUrl: 'settings.html',
})
export class SettingsPage{
constructor(public navCtrl: NavController, public navParams: NavParams, private toastCtrl: ToastController) {
}
//THIS IS THE LINE THAT DID THE TRICK OF SHOWING ION-TOGGLE AS PER item value
storageStatus: string = window.localStorage.getItem('item');
updateItem(){
window.localStorage.setItem('item', this.storageStatus);
this.presentToast();
}
presentToast() {
console.log("present Toast");
const toast = this.toastCtrl.create({
message: 'New Changes Updated',
duration: 3000,
position: 'bottom'
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
}
html file
<ion-content padding>
<ion-item-group>
<ion-item>
<ion-label>Item Status</ion-label>
<ion-toggle [(ngModel)]="storageStatus"></ion-toggle>
</ion-item>
</ion-item-group>
<button ion-button block color="light" (click)="updateItem()">Save Changes</button>
</ion-content>