2016-06-09 41 views
1

在角度应用程序中,我有2个页面,每个页面根据用户的privileged级别。因此,如何通过使用resolve或不使用从router重定向模板?Angular-route:如何从`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" 
       } 
      } 
     } 
}) 

回答

1

一个更好和干净的方式是有2条路线,说/secretpage/secretless和重定向基于特权usi ng以下路线配置:

$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') 
     } 
     } 
    } 

    }) 
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