gpt4 book ai didi

javascript - 登录后angularjs重定向

转载 作者:行者123 更新时间:2023-11-30 05:38:32 26 4
gpt4 key购买 nike

我可以知道如何在登录后将用户重定向到他们原来所在的位置吗?假设我有一个帖子 wwww.example.com/post/435 并且它要求用户登录才能“喜欢/评论”该帖子,所以在用户登录后我如何将用户重定向回来去邮局?我试过了

 $http.post('/sessionHandler/login',$scope.form).
success(function(data){
history.back(); // bring user back to previous path
}).error(function(err){
$scope.errorMessage = err;
});

然而,这并不是真正有效,如果用户之前的路径是其他站点或浏览器的新选项卡,它会将用户重定向回他们所在的位置。

我还通过使用 $locationChangeStart 找到了一些示例,但我在理解它是如何工作时遇到了一些困难,这就是我目前所拥有的 $locationChangeStart

$http.post('/sessionHandler/login',$scope.form).
success(function(data){
$rootScope.$on("$locationChangeStart", function (event, next, current) {
$location.path();// how to access to previous url where the user from ?
// how to use event, next and current to get the user last
// path on my site, and how to determine the last site is
// on my site but not other website ?
});

})

感谢您的帮助!!

最佳答案

可能不是最好的答案,但我为此使用了一个 cookie - 所以每当我重定向到一个新位置时,我都会使用 $cookies.loginDestination = $location.path(),然后每当用户登录时,我都会使用类似的东西:

var loginDestination = $cookies.loginDestination || '/';
$cookies.loginDestination = null;
$location.path(loginDestination);

关于javascript - 登录后angularjs重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com