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

2020-02-08 05:53发布

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"

3条回答
劳资没心,怎么记你
2楼-- · 2020-02-08 05:55

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();
    });
};
查看更多
戒情不戒烟
3楼-- · 2020-02-08 06:08

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();
查看更多
Fickle 薄情
4楼-- · 2020-02-08 06:15

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.

查看更多
登录 后发表回答