ui-router: Due to fix in 0.2.13 .when('/parent', '/parent/child') is not working with ui-sref
I would say, that the _“fix”_ in 0.2.13, related to: Avoid re-synchronizing from url after .transitionTo, change: 19715d15 introduced a new issue. Or at least, the .when('/parent', '/parent/child')
is not working for ui-sref="parent"
Description of the “issue”
As documented here: How to: Set up a default/index child state small extract:
Or if you want the ‘parent.index’ url to be non-empty, then set up a redirect in your module.config using $urlRouterProvider:
$urlRouterProvider.when('/parent', '/parent/index');
$stateProvider
.state('parent', {url: '/parent', abstract: true, template: '<ui-view/>'} )
// url '/parent/index'
.state('parent.index', {url: '/index'} )
NOTE: in the above cite, I replaced the home
in url with parent
to make it more consistent
This has stopped working. There are plunkers:
Examples
There are examples of the issue with different versions:
- working with version 0.2.12
- not working with version 0.2.13
- working with rolled back version 0.2.13 - if commented out the fix mentioned above
this is the change applied in “roll back” of a fix 19715d15 0.2.13 inside of the released angular-ui-router.js
1988 ...
1989 // RKö - rollback of a fix
1990 // if (ignoreUpdate) return true;
1991 ...
Workaround without change of 0.2.13
I described how to go around here Angular UI-Router $urlRouterProvider .when not working when I click <a ui-sref=“…”>
, we need eventing:
var onChangeConfig = ['$rootScope', '$state',
function ($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState) {
if (toState.name === "parent") {
event.preventDefault();
$state.go('parent.index');
}
});
}]
Maybe this is the way to go (or please, give me better). But as mentioned below, not sure about consistency…
Question, is it an issue? or not
I can imagine that this issue would be refuesd as proper behaviour.
BUT, is it really what we want - if this will result in 2 different states:
<a ui-sref="parent"> // goes to parent
<a href="#/parent"> // goes to parent.index
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 45 (5 by maintainers)
@LordWingZero
I’m assuming the promise from your redirectTo would still return one of the two options: the state name, or { state, param } object.
I would probably change the listener to this to support promises:
RedirectTo would look like this:
Alternatively you could use a resolve to do the redirect. We do this as well, which already supports promises and injecting resolves (parents and self).
@LordWingZero
Accessing parent resolves is a little difficult, the process of running the resolves happens inside the $state.transitionTo and are not accessible through the event. Accessing the toStates resolves will likely not be possible at all, since redirectTo is processed before the toStates resolves are run.
But if you can use a registered service instead of a parent resolve then below will work for you.
We’ve changed our method above to support dependency injection. My code above has been changed to:
Here is a sample of using it:
The ui-router team is looking to add this functionality natively #1235. My guess is parent resolves will still be unavailable. RedirectTo will likely occur before any resolves are run. Thoughts @christopherthielen?
I admire that… you are magicians. I have to touch it, start to test… upgrade… The idea about migrating to Typescript is MUST. We are on Typescript for 1,5 year… cannot imagine JS development without it anymore. Looking forward experience with the mighty 1.0 😉
In my case the underlying issue is in fact a feature request: we need a way to specify default child states.
Given:
It would be nice if we could say something like:
So that when we navigate to the abstract parent state:
We would get redirected (or even better: go directly) to
settings.general
.I used to do this using
when
but it would certainly be nicer to have a “isDefaultChild
” property on each state.What do you think?
Thanks