angularjs - Angular JS & Phonegap back button event -
i have mainctrl containing backstack history, 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 same controller i've got goback() function i'd call when button pressed;
$scope.goback = function () { $scope.urlhistory.pop(); $location.path($scope.urlhistory[$scope.urlhistory.length - 1]); };
this supposed work because index.html call ng-click="goback()" , works expected.
i use device events, same controller;
function onbackkeydown() { alert("back"); $scope.goback(); } document.addeventlistener("deviceready", ondeviceready, false); // device apis available // function ondeviceready() { // register event listener alert("deviceready"); document.addeventlistener("backbutton", onbackkeydown, false); };
i can see alerts, app's not navigating back. happens though, because if press device button twice, ng-click="goback()" takes me start page of app. using $scope wrong? experiencing on win phone & android. using phonegap build.
edit: i'm after here why $scope.goback(); function work differently when called devicelistener.
this solved it:
$scope.goback = function () { if (urlhistory == '/') { document.removeeventlistener("backbutton", onbackkey, false); }; urlhistory.pop(); $location.path(urlhistory[urlhistory.length - 1]); $scope.$apply(); };
the scope.$apply(); answer. it's necessary when executing angular expressions outside of angular. (angularjs $location not changing path)
Comments
Post a Comment