Angularjs $routeProvider中的条件表达式
我有一个使用有效的身份验证系统的应用程序。我这里的问题是,当用户连接时,他仍然可以通过URL访问登录页面。我想将所有连接的用户重定向到主页,而不是登录页面 因此,当有人要求Angularjs $routeProvider中的条件表达式,angularjs,session,authentication,angular-routing,Angularjs,Session,Authentication,Angular Routing,我有一个使用有效的身份验证系统的应用程序。我这里的问题是,当用户连接时,他仍然可以通过URL访问登录页面。我想将所有连接的用户重定向到主页,而不是登录页面 因此,当有人要求/authentication/login时: 如果用户已连接,他将被重定向到/home 如果无人连接,则打开对身份验证/登录的访问 这是我的实际工作代码(它不会将连接的用户重定向到主页) 是否可以在上述代码中添加一个条件语句? 比如: $routeProvider.when('/authentication/logi
/authentication/login
时:
- 如果用户已连接,他将被重定向到
/home
- 如果无人连接,则打开对
身份验证/登录的访问
这是我的实际工作代码(它不会将连接的用户重定向到主页)
是否可以在上述代码中添加一个条件语句? 比如:
$routeProvider.when('/authentication/login/', {
if(UserSession.getUser() != null) {
// Go to Home page instead
} else {
// Normal way
}
});
您可以这样做条件语句
angular.module('authentication').config(RouteConfig);
RouteConfig.$inject = ['$routeProvider', 'UserSession'];
function RouteConfig($routeProvider, UserSession) {
$routeProvider.when('/authentication/login/', {
templateUrl: 'section/authentication/login.tmpl',
controller: 'LoginController',
controllerAs: 'lo',
resolve: {
factory: checkRouting
}
});
}
var checkRouting= function ($q, $rootScope, $location) {
if ($rootScope.userProfile) {
return true;
} else {
var deferred = $q.defer();
$http.post("/yourUrl",data)
.success(function (response) {
$location.path("/login");
deferred.resolve(true);
})
.error(function () {
deferred.reject();
$location.path("/home");
});
return deferred.promise;
}
};
您可以这样做条件语句
angular.module('authentication').config(RouteConfig);
RouteConfig.$inject = ['$routeProvider', 'UserSession'];
function RouteConfig($routeProvider, UserSession) {
$routeProvider.when('/authentication/login/', {
templateUrl: 'section/authentication/login.tmpl',
controller: 'LoginController',
controllerAs: 'lo',
resolve: {
factory: checkRouting
}
});
}
var checkRouting= function ($q, $rootScope, $location) {
if ($rootScope.userProfile) {
return true;
} else {
var deferred = $q.defer();
$http.post("/yourUrl",data)
.success(function (response) {
$location.path("/login");
deferred.resolve(true);
})
.error(function () {
deferred.reject();
$location.path("/home");
});
return deferred.promise;
}
};
你可以用这个
$routeProvider.when('$routeProvider.',
{
redirectTo: function (routeParams, path, search) {
console.log(routeParams);
console.log(path);
console.log(search);
return "/";
}
})
尝试在return语句中有条件地返回主路由。
有关更多信息,请参阅文档中的重定向,或查看此处的您可以使用此链接
$routeProvider.when('$routeProvider.',
{
redirectTo: function (routeParams, path, search) {
console.log(routeParams);
console.log(path);
console.log(search);
return "/";
}
})
尝试在return语句中有条件地返回主路由。
有关更多信息,请参阅文档中的redirectTo或查看此处的我在一个角度应用程序中有一个非常类似的方法。在路由配置中,您可以添加任何您喜欢的密钥,并使用
$routeParams
服务访问这些密钥
以你为例:
function RouteConfig($routeProvider, UserSession) {
$routeProvider.when('/authentication/login/', {
templateUrl: 'section/authentication/login.tmpl',
controller: 'LoginController',
controllerAs: 'lo',
blockAuthenticated: true // <-- add a new flag, something like this
});
}
在我的一个角度应用中,我有一个非常类似的方法。在路由配置中,您可以添加任何您喜欢的密钥,并使用
$routeParams
服务访问这些密钥
以你为例:
function RouteConfig($routeProvider, UserSession) {
$routeProvider.when('/authentication/login/', {
templateUrl: 'section/authentication/login.tmpl',
controller: 'LoginController',
controllerAs: 'lo',
blockAuthenticated: true // <-- add a new flag, something like this
});
}
您可以将以下代码理解为: 在每次路由更改时,“如果连接的用户想要访问
/authentication/login/
,然后他将被重定向到/home
”
此解决方案的灵感来源于。您可以将以下代码理解为: 在每次路由更改时,“如果连接的用户想要访问
/authentication/login/
,然后他将被重定向到/home
”
此解决方案的灵感来源于。我不明白预期结果是什么,你能再解释一下你的解决方案吗?我不明白预期结果是什么,你能再解释一下你的解决方案吗?谢谢,但下次提供。谢谢,但下次提供。