I'm creating a tab based page which shows some data. I'm using UI-Router in AngularJs to register states.
My aim is to have one default tab open on page load. Each tab have sub tabs, and I would like to have a default sub tab open when changing tabs.
I was testing with onEnter
function and inside I'm using $state.go('mainstate.substate');
but it seems not to work due to loop effect issues (on state.go to substate it calls its parent state and so on, and it turns in a loop).
$stateProvider
.state('main', {
url: '/main',
templateUrl: 'main.html',
onEnter: function($state) {
$state.go('main.street');
}
})
.state('main.street', {
url: '/street',
templateUrl: 'submenu.html',
params: {tabName: 'street'}
})
Here I created a plunker demo.
For now everything works, except that I don't have default tab open and that's exactly what I need.
Thank you for your suggestions, opinions and ideas.
In fact even if this solution proposed by Radim did exactly the job, I needed to remember every tab's sub tab (state).
So I found another solution which do the same thing but also remembers every tab substate.
I all have to do was to install ui-router-extras and use the deep state redirect feature:
Thank you!
Since version 1.0 of ui-router, a default hook has been introduced using IHookRegistry.onBefore() as demonstrated in the example Data Driven Default Substate in http://angular-ui.github.io/ui-router/feature-1.0/interfaces/transition.ihookregistry.html#onbefore
As of version 1.0 of the ui-router it supports a
redirectTo
property. For example:If someone comes here for an answer regarding how to implement a simple redirect for a state and, as it happened to me, doesn't have the opportunity to use the new router...
Obviously again if you can, @bblackwo answer is great:
If you can't then just manually redirect it:
I hope it helps!
I created an example here.
This solution comes from a nice "Comment" to an an issue with redirection using
.when()
(https://stackoverflow.com/a/27131114/1679310) and really cool solution for it (by Chris T, but the original post was by yahyaKacem)https://github.com/angular-ui/ui-router/issues/1584#issuecomment-75137373
So firstly let's extend main with redirection setting:
And add only this few lines into run
This way we can adjust any of our states with its default redirection...Check it here
EDIT: Added option from comment by @Alec to preserve the browser history.