2017-07-06 23 views
1

私はバックボタンイベントをインターセプトしたいと思います。 リスナーが呼び出されると、すぐにバック信号がトリガーされます。イオン3ブロックバックボタンイベント

これは私が試したものです:

document.addEventListener("backbutton",() => { 
     console.log("[WARN] BackBtn pushed"); 
     console.log("TextboxVal: " + this.inputValue); 
     this.showConfirm(); 
     document.removeEventListener("backbutton"); 
    }); 

私は "this.inputValue" 値を失いたくありません。 しかし、ほとんどの場合、すでに消えてしまっています。

+0

クッキーhttps://www.npmjs.com/package/cookies-jsとリアクティブフォームについて考えてみてください。 https://angular.io/guide/reactive-formsあなたが離れると状態を戻すことができればそのように保存できます。 Angus UniversityのVascoはRxJSのチュートリアルビデオでこれについて何かしました。 https://angular-university.io/lesson/angular-rxjs-reactive-patterns-reactive-forms-draft-data-saving-implementationを参照してください。実際にはform.valueChangesを使用し、フォームがフィルタを使用して有効な状態 – JGFMK

+0

これを修正するにはいくつかの方法がありますが、最終的にどのような要件があるのか​​分かりません。 a)値をストレージに保存します。その結果、ユーザーがページに戻ったときに値が表示されます。b)ページを閉じる前にアラートを表示します。ユーザーがまだ退出したい場合は、その入力をクリアして戻る前のページに移動します。これらのいずれかがうまくいく場合は、私に知らせてください。 – sebaferreras

+1

ページを離れる前にアラートを表示したい。 – MrFlyingToasterman

答えて

0

ページを閉じる前にアラートを表示したいと思います。

あなたはそのためのNavGuards使用することができますいくつかのケースでは

を、開発者が残しビューと 入りを制御することができるはずです。これを可能にするために、NavControllerは、ionViewCanEnter およびionViewCanLeaveの方法を有する。角のルートガードに似ていますが、 はNavControllerと統合されています。私は自分でそれを行ってどのように

// ... 

    ionViewCanLeave() { 

    if(this.inputValue) { 
     // Show the alert 
     this.presentConfirm(); 
    } 

    // Will leave if the input is null 
    return this.inputValue === null; 
    } 

    presentConfirm() { 
    let alert = this.alertCtrl.create({ 
     title: 'Exit', 
     message: 'Do you want to exit?', 
     buttons: [ 
     { 
      text: 'Cancel', 
      role: 'cancel' 
     }, 
     { 
      text: 'Exit', 
      handler:() => { 

      // Allow the user to exit this page 
      this.inputValue = null; 

      // Go back to the previous page 
      setTimeout(() => { this.navCtrl.pop(); }, 500); 

      } 
     } 
     ] 
    }); 
    alert.present(); 
    } 
0

ザッツ:私は私自身のカスタムBackBTN方法を設定することができます。これにより

constructor(public plt: Platform) { 
    //Catch BackBTN Event 
     plt.ready().then(()=> { 
      plt.registerBackButtonAction(()=> { 
      this.showConfirm(); 
      }) 
     });​ 
} 

showConfirm() { 
     let confirm = this.alertCtrl.create({ 
     title: this.confirm, 
     message: this.confirm_msg, 
     buttons: [ 
      { 
      text: this.disagree, 
      handler:() => { 
       console.log("Saved changes"); 
       this.dismiss(); 
      } 
      }, 
      { 
      text: this.agree, 
      handler:() => { 
       console.log("Discard changes"); 
       this.viewCtrl.dismiss(); 
      } 
      } 
     ] 
     }); 
     confirm.present(); 
    } 
    } 

+0

このアプローチの問題は、その動作がそのページだけでなく、すべてのページに対して設定されることです。カスタムバックボタンハンドラの登録を取り消そうとすることもできますが、スタックから複数のページをプッシュ/ポップすると、アプリのバグが発生することがあります。 – sebaferreras