2017-05-25 10 views
0

私は確認できるチェックボックスを持っています。だから彼らがアラートに同意すると、すべての演習が完了するようになります。チェックボックスをオンにすると、一度だけ警告が表示されます。

問題:私は、ローカルストレージアイテム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) 
    }  
    } 

そして、ここで私はgetItem methodを呼び出しますin ionViewWillEnter

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

この問題を解決するにはどうすればよいですか一度あなたがそれをクリックし、アプリを再び開くよりも、この同じアラートを表示せずにチェックボックスをtrueに設定しますか?

+0

'localForage.getItem'の' this.cheatCheckbox = true;を設定すると 'cheatButton'関数がトリガされますか? – Ari

+0

@Ari yes 'cheatButton'関数はtrueのチェックボックスでトリガーされます – Sreinieren

答えて

0

ionViewWillEnterではなく、constructorlocalForage.getItemと記載します。それはあなたの問題を解決しなかった場合、あなたは、checkBoxInitializedと呼ばれるフラグを使用constructor trueに、それを初期化し、その後、次のようにそれを使用することができます。

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

その後にcheatButtonを変更します。

cheatButton(cheatCheckbox: boolean){ 

    if(cheatCheckbox && this.checkBoxInitialized){ 
    ... 

    }else 
     this.checkBoxInitialized = true; 

ところで、なぜcheatCheckboxcheatButtonに渡すのですか? this.cheatCheckboxはいつでも使用できます。

+0

しかし、再びアプリケーションを開いて、すべての演習が行われているので、' value = true'よりも警告が表示されます。 – Sreinieren

+0

私は自分の答えを編集しました。このバージョンを試すことができます。 – Ari

関連する問題