Skip to content

How to get current route within resolve function? #3273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
geoidesic opened this issue Jan 17, 2017 · 2 comments
Closed

How to get current route within resolve function? #3273

geoidesic opened this issue Jan 17, 2017 · 2 comments
Labels
Milestone

Comments

@geoidesic
Copy link

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")?

@geoidesic
Copy link
Author

http://stackoverflow.com/questions/41703119/how-to-get-current-route-within-resolve-function

@christopherthielen
Copy link
Contributor

christopherthielen commented Jan 20, 2017

in 1.0.0-rc.2 you will be able to inject the state context for the resolve as $state$. Example: http://plnkr.co/edit/sAOIzOp1HYlBmgivAPRe?p=preview

You can also inject $transition$ to get access to things like the "to" state, i.e.: $transition$.to().

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.

Also note that in 1.0, this is not a good use of resolve. This is the perfect use of a transition hook such as an onStart hook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants