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

Angular-route : how to switch template from `resolve` method?

在 angular 应用程序中,我有 2 个页面,每个页面都根据用户的 privileged 级别。为此,如何使用 resolve 或不使用 routerrouter 重定向模板?

正确的方法是什么?

这是我要找的东西:

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

保护页面的常用方法是使用路由更改前广播的 $routeChangeStart 事件:

angular.module('myApp', [])
.run(function($rootScope, $location, accessFac) {
    $rootScope.$on('$routeChangeStart',
        function(evt, next, curr) {
            if (!accessFac.checkPermission()) {
                $location.path('/login'); // redirect
            }
    })
});

更好更简洁的方法是使用 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')
        }
      }
    }

  })