Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current route within resolve function?

I have a service which checks whether the current user has access to the route. I would like that service to be called as part of the resolve, so that the view never loads if the user should not have access. However, within the resolve function the $state dependency does not yet contain the actual state of the router.

.state('home', {
  url: '/home',
  templateUrl: 'app/home.html',
  controller: 'HomeController',
  controllerAs: 'main',
  resolve: {
    allowed: function(auth, $state) {
      return auth.isAllowed($state.current.name);
    }
  },
})

... however $state.current.name is empty at the point which it is used. How can I pass the state's name (i.e. "home")?

like image 417
geoidesic Avatar asked Jan 17 '17 17:01

geoidesic


Video Answer


2 Answers

Try with

resolve: {
   allowed: function(auth) {
      return auth.isAllowed(this.self.name);
   }
},
like image 50
Dario Avatar answered Oct 12 '22 05:10

Dario


For UI router 1.0+: inject $state$ to get access to future state (to which transition is in progress). However, if resolve is in parent or abstract state, it will point to that abstract state, not child state to which transition is.

resolve: {
    allowed: function($state$) {
    ...
    }
}
like image 35
setec Avatar answered Oct 12 '22 03:10

setec