Angular JS & Phonegap Button Event Back

I have a MainCtrl containing a still frame for the story, for example:

$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 have a GoBack () function that I would like to call when the back button is pressed,

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

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

I use this for device events of the 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 see warnings, but the application does not move backward. Something happens because if I double-click the device button back, ng-click = "goBack ()" suddenly brings me back to the application start page. Am I using $ scope incorrectly? Testing this on Win Phone and Android. Using the Phonegap assembly.

edit: , $scope.goBack(); - devicelistener.

+2
2

:

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

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

. $apply(); . angular angular. ( AngularJS $ )

+1

. :

function onDeviceReady() {
    document.addEventListener("backbutton", function () {
        window.history.go(-1);
    });
}

, , angular. - ...

:. , , backbutton , ... - ?

0

Source: https://habr.com/ru/post/1605629/


All Articles