Angular UI Router pass parameter to .go

Published on 2017-11-22 by Raul

I ran today into a tricky way of using ui-router: if you pass a parameter to go('state'), it doesn't arrive in the controller that handles that state. This is because you need to declare the parameter in the state, for it to be taken into account. If you don't do this, the parameter won't arrive in the controller. 

 

So, when declaring the route:

$stateProvider.state('app.page', {
url: '/page/:param',
....
});

 

And the redirect call:

$state.go('app.page', { param: 'test' });

In the controller, you can get the parameter using $stateParams.param (make sure you declare $stateParams in the controller's constructor, in order for it to get injected).

 

 


Keep in touch


About

Hey there, I'm Raul, owner of CreativeCLR, a small consulting company.

Feel free to drop me any question you might have on this post using the comment section or the contact page.