Angular JS应用设备后退按钮必须具有确认对话框

Angular JS application device back button must have confirm dialog

本文关键字:对话框 确认 按钮 应用 JS Angular      更新时间:2023-09-26

我正在做一个AngularJS移动应用程序,它有一些模块。我需要在我的应用程序中访问设备的后退按钮,对话框必须具有"确定"和"取消"。

  1. 单击"确定"时,必须关闭应用程序。
  2. 单击"取消"时,它必须关闭对话框或返回 false。

我尝试了很多方法,但无法按预期得到结果。请任何人建议我解决这个问题。我喜欢以任何可能的方式完成此操作。请看我在堆栈溢出中的另一个问题。我已经给出了一些用于此问题的编码。

AngularJS设备后退按钮不起作用。?

我更喜欢使用$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

使用最适合您情况的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
}
});

希望这应该有所帮助!

您可以使用 ionic 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();
         }
        })
     }
}); 

感谢支持人员,我已经通过获得伙伴的帮助完成了这个问题。请参阅下面的代码。:)

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");
	if(r==true){
		console.log("not exit");
		navigator.app.exitApp();
	}else {
     navigator.app.goBack();
    }
}else {
    /* $ionicHistory.goBack(); */
	window.history.back();
    navigator.app.goBack();
}
}
}, 100);
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
    $rootScope.title = current.$$route.title;
  });
}]);