【发布时间】:2014-09-12 12:36:57
【问题描述】:
我有这样的路线设置:
app.config(function($routeProvider) {
$routeProvider
//login
.when("/", {
templateUrl : "framework/views/login.html",
controller : "LoginCtrl",
title: "Login",
authenticate: false
})
//dashboard
.when("/dashboard", {
templateUrl : "framework/views/dashboard.html",
controller : "DashboardCtrl",
title: "Dashboard",
authenticate: true
});
});
如果authenticate 在路线上设置为true,但session variable 不是true,现在我想重定向位置更改。
例如:
$rootScope.$on("$locationChangeStart", function(event, newURL, oldURL){
if (toState.authenticate && $window.sessionStorage.isLoggedIn) {
$location.path("/");
}
});
如果我改用$routeChangeStart,这会起作用,但是在重定向之前我会短暂看到下一条路线。位置更改似乎阻止了这种情况,但我不知道如何访问路由参数(即身份验证参数)。
我该怎么做?还是完全有更好的方法?
【问题讨论】:
-
Imo 在这种情况下最好使用拦截器docs.angularjs.org/api/ng/service/$http
-
@Whisher 很有趣。你能提供一个示例答案吗?
-
在 app.js 中的 .run(function(){ //here }) 中添加这个博客
-
@AdityaSethi 什么博客?
-
@Cooper 他的意思是你是在
.run方法块中运行你的代码吗?
标签: javascript angularjs routes