I'm using ui-router with nested states/views and I'm having a problem with the back button.
State hierarchy
- product
- personal
- sub-cat-1
- sub-cat-2
- business
- sub-cat-a
- sub-cat-b
- personal
- search
Problem
I initially load the page in state product.business.sub-cat-a. The top level page has a search input form which, when used, causes a state transition to state search (via a call to $state.go()) which all works fine.
However, if I click on the back button, I see the state transition from search to product.business.sub-cat-a as expected (via a call to $state.transitionTo()), but then there is an additional call to $state.go() back to state search and I can't work out where this call is coming from.
Would appreciate some help if anyone can shed some light on what ui-router is doing
Update
I've traced the problem down to the interaction between the urlRouter and $location. Seems $urlRouter.update sometimes pushes a url defined in a stat to $location and sometime pulls the value out of $location.
In this case, when things go wrong, the urlRouter location value is the old url which overwrites the $location.url value and causes the urlRouter to load the old state rather than the state we are trying to go back to.
The sequence seems to be:
- click back button
- browser loads the previous url from it's history
- ui-router goes about loading the correct state (the previous state)
- update is called on
urlRouter(without read parameter) which causes$locationto be reset to the pre-back button state url based on the urlRouter location value urlRouterthen sees the two urls/states as different so it reloads the state now specified by$location.urlwhich is the pre-back button url
So the problem seems to be that urlRouter location value isn't updated when the state is updated in step 3 above.
Any ui-router/angular experts, please feel free to point me in the right direction.