简体   繁体   English

离子 registerBackButtonAction 不起作用

[英]Ionic registerBackButtonAction does not work

Here are the steps to reproduce:以下是重现的步骤:

  1. Create a basic ionic project ionic start test sidemenu创建一个基本的 ionic 项目ionic start test sidemenu
  2. Add the android platform ionic platform add android添加android平台ionic platform add android
  3. In app.js add the code:app.js 中添加代码:

     $ionicPlatform.registerBackButtonAction(function (event) { alert("back button action handler"); event.preventDefault(); }, 999);

    This code can be added in the .run method or in the $ionicPlatform.ready() method - same result, not working可以在 .run 方法或$ionicPlatform.ready()方法中添加此代码 - 结果相同,但不起作用

  4. ionic build android then ionic upload -> or manually put the APK on a device ionic build android然后ionic upload -> 或手动将 APK 放在设备上

[BUG] - the alert is not shown and history view navigation is performed. [BUG] - 不显示警报并执行历史视图导航。 It's like this action that I try to register is not taken into consideration.就像我尝试注册的这个动作没有考虑在内。

What am I doing wrong?我究竟做错了什么? I tried this code in a controller also, also e.stopPropagation() or e.stopImmediatePropagation still no success.我也在控制器中尝试了这段代码,还有e.stopPropagation()e.stopImmediatePropagation仍然没有成功。

I have the latest Ionic (1.4.5) and Cordova 4.3.0, tested on some Samsung devices.我有最新的 Ionic (1.4.5) 和 Cordova 4.3.0,在一些三星设备上测试过。 In Ripple, it works ok.在 Ripple 中,它工作正常。

try preventing the default first首先尝试阻止默认设置

$ionicPlatform.registerBackButtonAction(function (event) { 
    event.preventDefault();
    alert("back button action handler");  
 }, 999);

Also the code 999 is perfectly valid, the codes 100, 200, 300, 400, 500 are just the priorities that ionic asigns to certain actions of the back button.代码 999 也是完全有效的,代码 100、200、300、400、500 只是 ionic 分配给后退按钮的某些操作的优先级。 I've used multiple times the priority 900, it just puts your backbutton on top of all other action.我已经多次使用优先级 900,它只是将您的后退按钮置于所有其他操作之上。

More information in the documentation : http://ionicframework.com/docs/api/service/ $ionicPlatform/文档中的更多信息: http ://ionicframework.com/docs/api/service/$ionicPlatform/

Can you use code 999?您可以使用代码 999 吗? Here that info about 100, 200, 300, 400, 500, but not 900. http://ionicframework.com/docs/api/service/ $ionicPlatform/这里说的信息约100,200,300,400,500,而不是900 http://ionicframework.com/docs/api/service/ $ ionicPlatform /

I use code for example 501 and this works.我使用例如 501 的代码,这有效。

That is example from my project which works这是我的项目中有效的示例

$scope.$on("$ionicView.enter", function () {
            $ionicPlatform.registerBackButtonAction(registerBackButtonAction, 501);
        });

  function registerBackButtonAction(e) {

            if (!!e && typeof e.preventDefault === 'function') {
                e.preventDefault();
            }

            // some code

            return false;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM