Javascript Ionic 2切换使用存储器保存数据

Javascript Ionic 2切换使用存储器保存数据,javascript,typescript,ionic-framework,ionic2,Javascript,Typescript,Ionic Framework,Ionic2,我有一个非常简单的问题,我正在构建一个应用程序,其中我有一个设置页面,带有切换输入,用户可以打开和关闭一些警报。我的问题是如何保存切换值,以便当有人关闭应用程序或导航到另一个页面时,切换状态不应丢失,并且当前切换应反映在html中。目前,我正在使用cordova sqlite存储插件来存储切换值 Page.html <ion-item> <ion-label>300 Units</ion-label> <ion-toggle [(ngMo

我有一个非常简单的问题,我正在构建一个应用程序,其中我有一个设置页面,带有切换输入,用户可以打开和关闭一些警报。我的问题是如何保存切换值,以便当有人关闭应用程序或导航到另一个页面时,切换状态不应丢失,并且当前切换应反映在html中。目前,我正在使用cordova sqlite存储插件来存储切换值

Page.html

<ion-item>
    <ion-label>300 Units</ion-label>
    <ion-toggle [(ngModel)]="AlertOn200"  [checked]="toggleStatus" (ionChange)="Change_Toggle_on_200();"></ion-toggle>
  </ion-item>
storage.ready()
上,您应该在类中设置
AlertOn200
和/或
toggleStatus
属性,而不是
val
属性。这应该将切换设置为存储中的值

编辑:我已经添加了一些代码,下面我已经测试并运行。 离子切换通过存储器中的值设置其初始值,如果手动切换离子切换,存储器中的值将更改

我建议删除离子切换组件中的
[checked]
绑定,并将切换模式设置为
toggleStatus
,如下所示:

<ion-toggle [(ngModel)]="toggleStatus" (ionChange)="Change_Toggle_on_200();"></ion-toggle>

如果随后实现以下类:

export class Page1 {
  toggleStatus: any;

  constructor(public navCtrl: NavController, public storage: Storage) {
    storage.ready().then(() => {
      storage.get('toggleStatus').then((val) => {
        console.log(`Setting status from storage to '${this.toggleStatus}'`);
        this.toggleStatus = val; // <-- Just set this.toggleStatus to the value from storage, this will make the ion-toggle change value from default to value from storage
      })
    });
  }

  Change_Toggle_on_200() {
    console.log(`changing toggleStatus to '${this.toggleStatus}'`);
    this.storage.set('toggleStatus', this.toggleStatus); // <-- note we can just set the current value of this.toggleStatus as this changes with the toggle
  }
}
导出类第1页{
切换状态:任意;
构造函数(公共navCtrl:NavController,公共存储:存储){
storage.ready()。然后(()=>{
storage.get('toggleStatus')。然后((val)=>{
log(`Setting status from storage to'${this.toggleStatus}``);

this.toggleStatus=val;//所以..问题是?代码不起作用,当我导航到另一个页面并返回时,toggleStatus值没有保存。请再次阅读我的问题。
storage
是访问插件的自定义提供程序吗?我已经更新了页面。ts,存储来自cordova sqlite存储插件。lemme ask,can您在storage.ready()上处理切换事件,而不是在函数内部处理。抱歉,我仍然不清楚…您能否用代码解释:-)我将尝试在约20分钟内用一些代码响应感谢它非常有效:-)因此,如果您有ngModel,则无需使用[checked]?无法在
storage.ready()
函数中处理离子切换组件的更改事件,这必须通过
(ionChange)=“change\u toggle\u on_200();”
从存储中获取初始值,但是,在
storage.ready()
函数中是可以的。我认为
[(ngModel)]
自行处理已检查的数据。
export class Page1 {
  toggleStatus: any;

  constructor(public navCtrl: NavController, public storage: Storage) {
    storage.ready().then(() => {
      storage.get('toggleStatus').then((val) => {
        console.log(`Setting status from storage to '${this.toggleStatus}'`);
        this.toggleStatus = val; // <-- Just set this.toggleStatus to the value from storage, this will make the ion-toggle change value from default to value from storage
      })
    });
  }

  Change_Toggle_on_200() {
    console.log(`changing toggleStatus to '${this.toggleStatus}'`);
    this.storage.set('toggleStatus', this.toggleStatus); // <-- note we can just set the current value of this.toggleStatus as this changes with the toggle
  }
}