Remove page from history, so “back” will work properly

走远了吗. 提交于 2019-11-27 12:14:09

问题


I have my app that you need to login to get in to the other pages.

so the first page is "login" and it checks if you are already logged, if so you will be redirected to the main page app, if not it will show you the login page.

now the problem is when the user is inside the logged page area, and he clicks back he will get to the "login" page and than redirected back to the main page, as he is logged in already.

So he is stuck in an infinite loop.

how can I remove the login page from the history.

just like in android "android remove activity from history stack"


回答1:


here is the solution!

simply use:

  $ionicHistory.nextViewOptions({
     disableBack: true
  });

example for login function:

$scope.login = function () {

Security.login($scope.cred.email, $scope.cred.password)
    .success(function(data) {
        Security.setUser(data.data[0]);
        $ionicHistory.nextViewOptions({
            disableBack: true
        });
        $state.go('posts', {}, {location: "replace", reload: true});
    }).error(function(data) {
        $scope.showAlert();
    });
};



回答2:


For a pure AngularJS way to accomplish this (rather than ionic or javascript in the other answers), use the $location service's replace() method (documentation) :

Use $location.url('/newpath'); or $location.path('/newpath'); as you normally would to do the redirection in angular. And then just add $location.replace(); right after it. Or you can chain the commands like this:

$location.url('/newpath').replace();



回答3:


Quick search: https://stackoverflow.com/a/8969975/185672

Top answer:

Instead of using window.location = url; to redirect,

try window.location.replace(url);.

after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.



来源:https://stackoverflow.com/questions/26441698/remove-page-from-history-so-back-will-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!