角JS&安培; PhoneGap的后退按钮事件 [英] Angular JS & Phonegap back button event

查看:91
本文介绍了角JS&安培; PhoneGap的后退按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含历史backstack一个MainCtrl,像这样;

I have a MainCtrl containing a backstack for history, like this;

$scope.urlHistory = [];

$scope.$on('$routeChangeSuccess', function () {
    if ($location.$$absUrl.split('#')[1] !== $scope.urlHistory[$scope.urlHistory.length - 1]) {
        $scope.urlHistory.push($location.$$absUrl.split('#')[1]);
    }
});

然后,在同一个控制器我已经得到了我想要的时候后退按钮是pssed $ P $调用GoBack的()函数;

Then, in the same controller I've got a GoBack() function I'd like to call when the back button is pressed;

$scope.goBack = function () {
    $scope.urlHistory.pop();
    $location.path($scope.urlHistory[$scope.urlHistory.length - 1]);
};

这应该从我的index.html,因为工作,我把它与NG点击=GoBack的(),一切工作正常。

This is supposed to work because from my index.html I call it with ng-click="goBack()" and everything works as expected.

我用这对设备的事件,同一个控制器;

I use this for the device events, same controller;

function onBackKeyDown() {
  alert("back");
  $scope.goBack();
}

document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
  // Register the event listener
  alert("deviceready");
  document.addEventListener("backbutton", onBackKeyDown, false);
};

我可以看到警报,但应用程序不是导航回来。有事尽管,因为如果我preSS装置后退按钮两次,NG-点击=GoBack的()一下子把我带回到该应用程序的起始页面。我使用$范围错了吗?在Win电话和放大器遇到此; Android系统。使用PhoneGap的生成。

I can see the alerts, but the app's not navigating back. Something happens though, because if I press the device back button twice, the ng-click="goBack()" suddenly takes me back to the start page of the app. Am I using $scope wrong? Experiencing this on Win Phone & Android. Using Phonegap Build.

编辑:我真的在这里后,什么是为什么我的$ scope.goBack();函数的工作从devicelistener叫不同的时。

推荐答案

这解决了这个问题:

  $scope.goBack = function () {
    if (urlHistory == '/') { 
      document.removeEventListener("backbutton", onBackKey, false);

    };    
    urlHistory.pop();
    $location.path(urlHistory[urlHistory.length - 1]);
    $scope.$apply();
  };

适用范围适用于$();是我的回答。角以外执行前角pressions时,这是必要的。 (<一href=\"http://stackoverflow.com/questions/11784656/angularjs-location-not-changing-the-path\">AngularJS $位置不改变路径)

这篇关于角JS&安培; PhoneGap的后退按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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