AngularJS:理解 $rootScope.$on('$routeChangeSuccess [英] AngularJS: Understanding $rootScope.$on('$routeChangeSuccess

查看:23
本文介绍了AngularJS:理解 $rootScope.$on('$routeChangeSuccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理登录页面,成功后,它重定向到主页.默认情况下,我显示登录页面此代码:

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..')
    };
  };

如果我删除 if 语句的 else 部分之前的第二个 $location.url,重定向似乎不起作用.然而,它没有使用那个 url (/blah),它转到 home.但如果 url blah 被删除,则重定向逻辑不起作用.

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().如果有人能帮助我了解这个重定向系统是如何工作的,我会很感激?

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-routinghttps://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=preview可能会好一点...

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 拦截器,当服务器返回未授权"错误代码时,它会重定向到登录页面.但这似乎是一种比您准备好的方法更高级的方法.

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.$on('$routeChangeSuccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆