Phonegap Android 后退按钮,带 Ionic 和 Angular [英] Phonegap Android Back Button w/ Ionic and Angular

查看:22
本文介绍了Phonegap Android 后退按钮,带 Ionic 和 Angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Ionic Framework/Angular JS 的 Phonegap 项目中禁用 Android 的后退按钮时遇到问题.我尝试了许多其他建议的解决方案,但无济于事.问题是我有一个登录"屏幕,它是一个模态 (ionicModal),但无论是否登录,Android 用户都可以使用后退按钮导航离开.

I'm having issues disabling the back button for Android in a Phonegap project using Ionic Framework / Angular JS. I have tried many other proposed solutions to no avail. The problem is I have a 'Login' screen which is a modal (ionicModal), but Android users are able to use the back button to navigate away regardless of if they are logged in or not.

我尝试同时禁用 Android 后退按钮.事件触发,但页面导航仍然发生.我觉得如果这行得通,这将是理想和最直接的解决方案.不过在这里, preventDefault() 和 stopPropagation() 似乎没有效果.

I tried disabling the Android Back button all together. The event fires, but the page navigation still happens. I feel like if this worked, this would be the ideal and most straight forward solution. Here though, preventDefault() and stopPropagation() seem to have no effect.

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false);
}

function onBackKeyDown(e) {
    alert('back button triggered');
    e.preventDefault();
    e.stopPropagation();
}

我还尝试通过侦听 $locationChangeStart 并防止这种情况从 Angular 中禁用导航:

I also tried disabling the navigation from within Angular by listening to $locationChangeStart and preventing that:

// Disable "Back" button on androids if user is on login screen
$rootScope.$on('$locationChangeStart', function(e) {
    if( true ) {
        e.preventDefault();
        e.stopPropagation();
    }
});

这似乎也有效,但不知何故仍然不能阻止 Android 隐藏模式并转到上一个屏幕.

This also seems to work, but yet somehow still does not prevent Android from hiding the modal and going to the previous screen.

在使用 Phonegap/Ionic/Angular 时,是否有适当的方法来禁用 Android 后退按钮?它使用 Angular UI 路由,模态不是路由而是 ionicModal.

Is there a proper way to disable the Android Back button when using Phonegap/Ionic/Angular? It uses the Angular UI router, and the modal is not a route but rather an ionicModal.

推荐答案

似乎有一个名为 "hardwareBackButtonClose" 可以在模态上设置,以确定是否可以使用 Android 和类似设备上的硬件后退按钮关闭模态.默认为:true,因此请尝试将其设置为 false.

seems that there is a boolean property named "hardwareBackButtonClose" that you can set on the modal to determine whether the modal can be closed using the hardware back button on Android and similar devices. Defaults to: true, so try setting it to false.

这篇关于Phonegap Android 后退按钮,带 Ionic 和 Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆