登录后angularjs重定向 [英] angularjs redirection after login
问题描述
我知道如何将用户,他们原本在登录后重定向到?说我有一个帖子 wwww.example.com/post/435
,它要求用户登录,以喜欢/评论的帖子,这样用户登录后我如何可以重定向用户回帖子?我已经试过这种
may I know how to redirect user to where they originally at after login ? say i have a post wwww.example.com/post/435
and it required user to log in in order to "like/comment" the post, so after the user login how can i redirect user back to the post ? i have tried this
$http.post('/sessionHandler/login',$scope.form).
success(function(data){
history.back(); // bring user back to previous path
}).error(function(err){
$scope.errorMessage = err;
});
然而,这是不是真的有效,如果用户previous路径是其他网站或浏览器新标签,将用户重定向到他们在哪里。
however this is not really effective, if a user previous path is other site or a browser new tab it will redirect the user back to where they are.
我也发现了一些例如通过使用 $ locationChangeStart
,但我有一些困难,了解它是如何工作这是我迄今为止的 $ locationChangeStart
i also found some example by using $locationChangeStart
but i'm having some difficulties to understand how it works this is what i have so far for $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 ?
});
})
您的帮助是AP preciated!
your help is appreciated !!
推荐答案
也许不是最好的答案,但我用这一个cookie - 所以每当我重定向到一个新的位置,我会使用 $ cookies.loginDestination = $ location.path()
,则当用户登录,我用的是这样的:
May not be the best answer, but I use a cookie for this - so whenever I'm redirecting to a new location, I'll use $cookies.loginDestination = $location.path()
, then whenever a user logs in, I use something like:
var loginDestination = $cookies.loginDestination || '/';
$cookies.loginDestination = null;
$location.path(loginDestination);
这篇关于登录后angularjs重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!