Angular-route:如何从`resolve`方法切换模板?

时间:2016-06-09 06:17:25

标签: angularjs angular-routing

在角度应用程序中,我有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"
                }
            }
        }
})

2 个答案:

答案 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
            }
    })
});

https://stackoverflow.com/a/11542936/2430054