在角度应用程序中,我有2个页面,每个页面都根据用户的privileged
级别。为什么我如何使用router
或不使用?{/ p>重定向resolve
模板
什么是正确的方法?
这就是我要找的东西:
$routeProvider.when('/secretpage' , {
templateUrl: null,
resolve:{
"check":function(accessFac,$location){
if(accessFac.checkPermission()){
//check if the user has permission -- This should happen before the page loads
return this.template: "templates/secretpage.html"
} else {
this.template: "templates/secretlesspage.html"
}
}
}
})
答案 0 :(得分:1)
更好更干净的方法是拥有2条路线,比如/secretpage
和/secretless
,并使用以下路线配置根据权限重定向:
$routeProvider
.when('/secretpage' ,{
templateUrl: "templates/secretpage.html",
resolve:{
"check":function(accessFac,$location){
if(!accessFac.checkPermission()){
$location.path('/secretless')
}
}
}
})
.when('/secretless', {
templateUrl: "templates/secretlesspage.html",
resolve: {
"check":function(accessFac,$location){
if(accessFac.checkPermission()){
$location.path('/secret')
}
}
}
})
答案 1 :(得分:0)
保护网页的常用方法是使用路由更改前广播的$routeChangeStart
事件:
angular.module('myApp', [])
.run(function($rootScope, $location, accessFac) {
$rootScope.$on('$routeChangeStart',
function(evt, next, curr) {
if (!accessFac.checkPermission()) {
$location.path('/login'); // redirect
}
})
});