2016-02-11 24 views
0

我正在做一個AngularJS移動應用程序,它有一些模塊。我需要在我的應用程序中訪問設備的後退按鈕,對話框必須有「確定」和「取消」。Angular JS應用程序設備後退按鈕必須有確認對話框

  1. 當單擊「確定」時,它必須關閉應用程序。
  2. 當單擊「取消」時,它必須關閉對話框或返回false。

我已經嘗試了很多方法,我無法得到預期的結果。請任何人建議我解決這個問題。我喜歡以任何方式完成此任務。請看Stack Overflow中的另一個問題。我已經給出了一些在這個問題中使用的編碼。

AngularJS device back button not working.?

回答

0

謝謝對於支持人員,我已經通過從我的隊友那裏獲得幫助來完成這個問題..請參閱下面的代碼.. :)

app.run(['$rootScope','$location', function($rootScope,$location) { 
 
    document.addEventListener("deviceready", function() { 
 
    console.log("deviceready"); 
 
    document.addEventListener("backbutton", onBackKeyDown, false); 
 
function onBackKeyDown(e) { 
 
    e.preventDefault(); 
 
    if ($location.path() === "/login" || $location.path() === "/home") { 
 
    var r=confirm("exit"); 
 
\t if(r==true){ 
 
\t \t console.log("not exit"); 
 
\t \t navigator.app.exitApp(); 
 
\t }else { 
 
    navigator.app.goBack(); 
 
    } 
 
}else { 
 
    /* $ionicHistory.goBack(); */ 
 
\t window.history.back(); 
 
    navigator.app.goBack(); 
 
} 
 
} 
 
}, 100); 
 
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) { 
 
    $rootScope.title = current.$$route.title; 
 
    }); 
 
}]);

0

使用SweetAlert這是完美的你的情況!

但是你需要導入一個JS文件sweet-alert.min.js

SweetAlert.swal({ 
       title: "Are you sure?", 
       text: "You want to go back!", 
       type: "warning", 
       showCancelButton: true, 
       confirmButtonColor: "#DD6B55", confirmButtonText: "OK", 
       cancelButtonText: "CANCEL", 
       closeOnConfirm: true, 
       closeOnCancel: true 
      }, function (isConfirm) { 
       if (isConfirm) { 
//you logic goes here 
} 
}); 

希望這應該有所幫助!

+0

感謝我會盡量恢復vkrishna .. :) –

+0

SweetAlert不defined..error –

+0

甜警告對話框也只有在應用程序關閉未來..? –

0

可以使用離子hardwareBackbutton如下:

$ionicPlatform.onHardwareBackButton(function(event) { 
    event.preventDefault(); 
    event.stopPropagation(); 
    if (true) { // your check here 
     $ionicPopup.confirm({ 
      title: 'System warning', 
      template: 'are you sure you want to exit?' 
     }).then(function(res) { 
     if (res) { 
      ionic.Platform.exitApp(); 
     } 
     }) 
    } 
}); 
+0

我已經使用這個我在onHardwareBackButton上我得到一些錯誤,我認爲.. –

+0

什麼錯誤?你有依賴關係添加$ ionicPlatform嗎? –

+0

發佈您的錯誤可能是我幫你 –

1

我更喜歡使用$ionicPlatform.registerBackButtonAction並檢查是否有意見之間的導航歷史(除了登錄頁面):

$ionicPlatform.registerBackButtonAction(function (e) { 
    if ($ionicHistory.backView() && $ionicHistory.backView().stateName != "login") { 
     // history back except login page 
     $ionicHistory.goBack(); 
    } else { 
     var confirmPopup = $myPopup.confirm({ 
       cssClass: 'center-txt-popup', 
       title: '<b>Closing app</b>', 
       template: "Do you want to exit?" 
      }); 
     confirmPopup.then(function (close) { 
      if (close) { 
       navigator.app.exitApp(); 
      } 
     }); 
    } 
    e.preventDefault(); 
    return false; 
}, 101); // 1 more priority than back button 
+0

感謝您的答案海狸..但爲什麼101 .. –

+0

優先101處理程序覆蓋'回到以前的觀點'默認。檢查離子文檔(答案中的鏈接)以獲取更多詳細信息 – beaver