Onsen用户界面:控制android backbutton路由:

时间:2022-08-24 13:12:37

I am developing an android application in cordova using Onsenui,in which i want to navigate to previous page when device backbutton is clicked I am using answer from this question to solve the issue

我正在使用Onsenui开发一个在cordova的android应用程序,在这个应用程序中,当我点击这个问题的答案来解决这个问题时,我希望导航到上一个页面。

Here is my code

这是我的代码

document.addEventListener("backbutton",onBackButtonPressed, false);
    function onBackButtonPressed(){
            alert('backbutton');
            var element = document.querySelector( ".navigator-container");
            var scope = angular.element( element ).scope();
            scope.popPage();
    }

I will get the following error

我将得到以下错误。

"Uncaught TypeError: Cannot read property 'popPage' of undefined"

“未捕获的类型错误:无法读取未定义的属性‘popPage’”

1 个解决方案

#1


3  

Probably .navigator-container has been deprecated, that's why I suggest you to get the navigator by using ons-navigator as argument for document.querySelector().

可能。navigator-container已经被弃用了,这就是为什么我建议您使用ons-navigator作为document.querySelector()的参数来获得导航器。

Here is the fixed code:

这是固定代码:

document.addEventListener("backbutton",onBackButtonPressed, false);
    function onBackButtonPressed(){
            alert('backbutton');
            var element = document.querySelector("ons-navigator");
            element.popPage();
}

EDIT

编辑

Supposing that you are using an older version of Onsen UI, like 1.3.4, and supposing that your navigator has a var='myNavigator' attribute, you can use the following code:

假设您使用的是Onsen UI的旧版本,如1.3.4,假设您的导航器有一个var='myNavigator'属性,您可以使用以下代码:

document.addEventListener("backbutton",onBackButtonPressed, false);
    function onBackButtonPressed(){
            alert('backbutton');
            var element = document.querySelector("ons-navigator");
            var scope = angular.element(element).scope();
            scope.myNavigator.popPage();
}

#1


3  

Probably .navigator-container has been deprecated, that's why I suggest you to get the navigator by using ons-navigator as argument for document.querySelector().

可能。navigator-container已经被弃用了,这就是为什么我建议您使用ons-navigator作为document.querySelector()的参数来获得导航器。

Here is the fixed code:

这是固定代码:

document.addEventListener("backbutton",onBackButtonPressed, false);
    function onBackButtonPressed(){
            alert('backbutton');
            var element = document.querySelector("ons-navigator");
            element.popPage();
}

EDIT

编辑

Supposing that you are using an older version of Onsen UI, like 1.3.4, and supposing that your navigator has a var='myNavigator' attribute, you can use the following code:

假设您使用的是Onsen UI的旧版本,如1.3.4,假设您的导航器有一个var='myNavigator'属性,您可以使用以下代码:

document.addEventListener("backbutton",onBackButtonPressed, false);
    function onBackButtonPressed(){
            alert('backbutton');
            var element = document.querySelector("ons-navigator");
            var scope = angular.element(element).scope();
            scope.myNavigator.popPage();
}