3gwebtrain 3gwebtrain - 6 months ago 20
AngularJS Question

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

In angular App, I have 2 pages, each of them for according to users

privileged
level. for that how can I redirect templates from
router
by using
resolve
or without?

what would be the correct way?

here is what I am looking for :

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

Answer

A better and clean way is to have 2 routes, say /secretpage and /secretless and redirect based on the privilege using below route configuration:

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

  })