选中复选框时仅显示一次警报



我有一个复选框,我可以检查它,然后在我询问他们是否确定知道的地方显示警报。因此,当他们就警报达成一致时,所有练习都变成了完成。

问题:我设置了一个本地存储项目didAllExercises所以当我重新打开应用程序时,我得到了这个项目,如果这是真的,则将复选框设置为 true,但我的复选框上有一个(onChange) event,并检查复选框是否被选中而不是显示警报。因此,每次我重新打开应用程序并且didAllExercises项为真时,都会显示警报

这是复选框:<ion-checkbox checked="false" [(ngModel)]="cheatCheckbox" (ionChange)="cheatButton(cheatCheckbox)"></ion-checkbox>

这是我cheatButton()

cheatButton(cheatCheckbox: boolean){
    if(cheatCheckbox){
      localForage.setItem("showAlert", true);
      console.log('ccTrue', cheatCheckbox);
      //DONT SHOW ALERT WHEN ALLEX ARE DONE AND YOU CLOSE AND OPENS THE APP AGAIN AND NAVIGATE TO OEFENINGEN
      setTimeout(() => {        
        localForage.getItem("showAlert", (err, value) => {
          if(value){
            this.showAlert = value;
            console.log('!notRun', value);
          }
        })
      },400)
      setTimeout(() => { 
              let alert = this.alertCtrl.create({
                  title: 'Voltooi alle oefeninen',
                  message: 'Weet je zeker dat je alle oefeningen gedaan hebt?',
                  buttons: [
                    {
                      text: 'Nee',
                      role: 'cancel',
                      handler: () => {
                        console.log('Cancel clicked');
                        this.cheatCheckbox = false;
                      }
                    },
                    {
                      text: 'Ja ik weet het zeker!',
                      handler: () => {
                        this.allExercisesDone = true;
                        localForage.setItem('didAllExercises', [true, this.data]);              
                      }
                    }
                  ]
                });
                alert.present();
      },400)
    }    
  }

在这里,我称ionViewWillEnter中的getItem method

 localForage.getItem('didAllExercises', (err, value) => {
    if(value){
      this.cheatCheckbox =  true;
     }
})

如何解决此问题以仅在单击警报后显示警报,然后重新打开应用程序,将复选框设置为 true 而不显示相同的警报?

与其ionViewWillEnter,不如在constructor中称localForage.getItem。如果这不能解决您的问题,您可以使用一个名为 checkBoxInitialized 的标志,在 constructor 中将其初始化为 true,然后按如下方式使用它:

localForage.getItem('didAllExercises', (err, value) => {
    if(value){
      this.cheatCheckbox =  true;
     } else {
      this.cheatCheckbox =  false;
     }
    this.checkBoxInitialized = false;
})

然后将作弊按钮修改为:

cheatButton(cheatCheckbox: boolean){
    if(cheatCheckbox && this.checkBoxInitialized){ 
    ...
    }else
       this.checkBoxInitialized = true;

顺便说一句,为什么要将cheatCheckbox作为参数传递给cheatButton?您可以随时使用 this.cheatCheckbox .

最新更新