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]);
}
});
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]);
};
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);
};
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.
edit: What I'm really after here is why would my $scope.goBack(); function work differently when called from the devicelistener.