2016-06-09 39 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