ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

Due to fix in 0.2.13 .when('/parent', '/parent/child') is not working with ui-sref

Open Radim-Kohler opened this issue 12 years ago • 45 comments

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:

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

Radim-Kohler avatar Nov 26 '14 04:11 Radim-Kohler

+1, I have the same issue.

dmitriynosenko avatar Nov 26 '14 07:11 dmitriynosenko

+2

see http://stackoverflow.com/questions/27120308/angular-ui-router-urlrouterprovider-when-not-working-when-i-click-a-ui-sref

J22Melody avatar Nov 26 '14 08:11 J22Melody

+1

As mentioned, this prevents redirecting URLs/states when using ui-sref. This is unfortunate as it now means I cannot upgrade past 0.2.12 since I require this functionality. I don't think we should have to clutter our $stateChangeStart event just to handle this, especially if we have multiple URL/state redirects in place. Hopefully this issue can be fixed in 0.2.14.

ashleyjfox avatar Nov 26 '14 15:11 ashleyjfox

+2 I have a "real world" example showing the same issue:

http://plnkr.co/edit/xyVPZePL2eV0lp8HxPBG?p=preview

it works with 0.2.12 but nOT with 0.2.13. If the new behaviour is intended I would like to know what is the recommended way for setting default states.

adestis-ds avatar Nov 26 '14 16:11 adestis-ds

People who are using .when this way: do you really want "state-based redirection"? .when is for matching a url and redirecting, and/or doing some other action. If this issue affects you, it means you are navigating using states, not url. I think what you are doing is navigating to a state (using state.go/ui-sref), but you really want to navigate to some other state (usually a substate).

In UI-Router the state is the primary focus, and the URL that maps to that state is secondary. In my eyes a state-based redirect is preferable to a url based one. Url redirection is more very loosely coupled and feels sloppy when compared to state-based redirection.

Read my analysis of the "double transition" problem here: #1573. As noted in the issue, avoiding re-sync is a safety net to avoid double-transitions. However, the primary fix is to ensure that all state-to-url mappings are bi-directional.

As such, since state based redirection using $stateChangeStart in 0.2.x is rather clumsy, I'm going to comment out the "avoid resync" logic for 0.2.14. I'm going to reintroduce it in 1.0 when we have a better state-level redirection API. If there are any other use cases that aren't "state-level redirection", please inform me so I make sure to account for them.

christopherthielen avatar Nov 27 '14 03:11 christopherthielen

+1 same problem

As for the use case: Better (i.e. non-central) state forwarding would be very welcome, also using the urlRouterProvider based forward for what really should be a state forward. The parent state is just a placeholder for us (which when called should redirect to a default child state) and we reference it in ui-sref to get "automagically" correct setting of the active class in the navigation. Since we have this use case in many places of our app we do not want to clutter the $stateChangeStart function with state redirects and thus opted to use $urlRouterProvider.

rguldener avatar Dec 04 '14 07:12 rguldener

:+1:

Same issue!

RobiFerentz avatar Dec 10 '14 06:12 RobiFerentz

If this issue affects you, it means you are navigating using states, not url. I think what you are doing is navigating to a state (using state.go/ui-sref), but you really want to navigate to some other state (usually a substate). @christopherthielen

For me, the reason I navigate to a parent state rather than its substate directly is because of ui-sref-active.

For example, I have a side menu item associated with, let's say, app.settings state that needs to be highlighted (with .active class) when I go to any of its substates (e.g. app.settings.a, app.settings.b, app.settings.c). app.settings state is just a common state encompassing those substates. So my html looks like:

<div ui-sref-active>
  <a ui-sref="app.settings">Settings</a>
</div>

When I click on Settings link, I want it to redirect me to the first substate (i.e. app.settings.a ) automatically. If I put app.settings.a directly in ui-sref above, Settings menu item will only be highlighted when the app is in app.settings.a but not when it's in app.settings.b or app.settings.c.

Maybe there is a better way of using ui-sref-active that I'm not aware of? Another workaround is using ng-class with $state.includes(), but I was hoping to make it work with ui-sref/ui-sref-active alone.

So if I want to keep <a ui-sref="app.settings">Settings</a> and have it redirect me to app.settings.a, I need $urlRouterProvider.when() work as before.

If there is a better way to handle the case above, I'd like to know as well. Thanks.

yaru22 avatar Dec 10 '14 23:12 yaru22

@yaru22 +1

Exactly the same scenario here!

fubupc avatar Dec 11 '14 10:12 fubupc

+1 Same issue

jeverden avatar Dec 11 '14 19:12 jeverden

+1

squadwuschel avatar Dec 16 '14 21:12 squadwuschel

+1

corbanbrook avatar Dec 18 '14 15:12 corbanbrook

:+1: I have the same issue, I use it like @yaru22 for ui-sref-active and for defining the default subroute with ui-router-extras deep state redirect

lazychino avatar Dec 18 '14 17:12 lazychino

+1

Zacharias3690 avatar Jan 07 '15 15:01 Zacharias3690

@christopherthielen another use case besides the one presented by @yaru22 3 main states home, profile and settings like this: qq

The 2nd state profile have 2 nested states profile.about and profile.main qqq

I need that the profile state clickable on the index page loads the profile view and redirects to the default nested state which is profile.about here qqqq

Here's a working plunker with what I got so far, with the fix mentioned by @Radim-Kohler.

yahyaKacem avatar Jan 12 '15 12:01 yahyaKacem

:+1:

ehacke avatar Jan 14 '15 16:01 ehacke

+1

acollard avatar Jan 15 '15 15:01 acollard

:+1:

ui-michal-szwed avatar Jan 16 '15 16:01 ui-michal-szwed

+1

goddyZhao avatar Jan 17 '15 03:01 goddyZhao

+1

summer-liu avatar Jan 19 '15 06:01 summer-liu