2017-05-25 68 views
0

我有一個複選框,我可以檢查並顯示一個提示,詢問他們是否確實知道。所以當他們同意警報時,所有的練習都會完成。在選中的複選框上只顯示一次提醒

問題:我設置一個本地存儲物品didAllExercises所以當我重新打開應用程序,我得到這個項目,如果這是真的不是設置複選框事實,但我對我的複選框,有一個(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 methodionViewWillEnter

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

如何解決此問題只顯示警告一旦你點擊它,並重新打開應用程序設置複選框爲true,而不顯示相同的警報?

+0

你是說在'localForage.getItem'中設置'this.cheatCheckbox = true;'會觸發你的'cheatButton'函數嗎? – Ari

+0

@Ari yes「cheatButton」函數被真正的 – Sreinieren

回答

0

而不是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到:

cheatButton(cheatCheckbox: boolean){ 

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

    }else 
     this.checkBoxInitialized = true; 

順便說一句,爲什麼你通過cheatCheckboxcheatButton作爲參數?您始終可以使用this.cheatCheckbox

+0

上的複選框觸發,但如果我重新打開應用程序,並且所有練習都如此完成,那麼'value = true'將會再次顯示警告 – Sreinieren

+0

我編輯了我的答案。你可以試試這個版本。 – Ari