AngularJS:了解$ rootScope上('$ routeChangeSuccess $ [英] AngularJS: Understanding $rootScope.$on('$routeChangeSuccess
问题描述
我正在一个登录页面,如果成功,它重定向到主页。默认情况下我显示登录页面此code:
I am working on a login page, on success, it redirect to home page. By default I show login page this code:
app.run(function($rootScope, $location) {
$rootScope.$on('$routeChangeSuccess', function() {
$location.url("/login");
});
});
然后验证用户之后/我从拿用户主页后台传递的细节:
Then after validating the user/pass details from the backend I take the user to the home page:
$scope.login = function() {
if ($scope.username === 'admin' && $scope.password === 'pass') {
console.log('successful')
$rootScope.$on('$routeChangeSuccess', function() {
$location.url("/home")
});
$location.url("/blah");
} else {
$scope.loginError = "Invalid username/password combination";
console.log('Login failed..')
};
};
重定向似乎没有,如果我删除第二个工作 $ location.url
的其他
部分之前的如果
语句。然而,不使用URL( /等等
),它进入首页
。但如果网址等等
被删除它重定向逻辑是行不通的。
The redirect doesn't seem to work if I remove the second $location.url
before the else
section of the if
statement. It is however not using that url (/blah
), it goes to home
. but If url blah
is removed it the redirect logic does not work.
我似乎无法理解为什么我必须使用两个 $ location.url()
。我想AP pretiate如果有人能帮助我理解这个重定向系统是如何工作的?
I can't seem to understand why I have to use two $location.url()
. I would appretiate if someone can help me understand how this redirect system works?
这可能不是最好的做法,我愿意就如何改进这一建议,这里是 Plunker例如
This might not be the best practice, I am open to suggestions on how to improve this, here is Plunker example
推荐答案
这所有的一切是IMO下去一个错误的道路...
All in all this is going down a wrong path IMO...
显然,你需要锁定的客户端总是可以在一个简单的调试器改成任何资源,服务器端...但我猜你已经知道...
Obviously you need to lock down any resources server side as client side can always be "changed" in a simple debugger... But I guess you already know that...
替代路由解决方案,如 https://github.com/dotJEM/angular-routing 或 https://github.com/angular-ui/ui-router IMO给你一些为了这个美好的把手,让刚刚评估的一些方法...
Alternative routing solutions like https://github.com/dotJEM/angular-routing or https://github.com/angular-ui/ui-router IMO gives you some better handles for this, but lets just evaluate some approaches...
其中之一是: http://plnkr.co/edit/ZUKB1v?p = preVIEW 虽然要求您解决所有路线的用户...所以..:(...
另一个办法是: http://plnkr.co/edit/iM9cA1?p=$p $ PVIEW 这可能是一个更好一点......
One would be: http://plnkr.co/edit/ZUKB1v?p=preview Although that requires you resolve the user on all routes... So.. :(... Another would be: http://plnkr.co/edit/iM9cA1?p=preview which might be a little better...
最后,人们似乎经常做的是提供HTTP拦截时,一个未经授权的错误code是从服务器返回一个重定向到登录页面。但是,这可能似乎比你准备好了更先进的方法。
Finally, what people often seem to do is provide http interceptors that redirects to the login page when a "Unauthorized" error code is returned from the server. But this could seem to be a more advanced approach than your ready for.
这篇关于AngularJS:了解$ rootScope上('$ routeChangeSuccess $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!