Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child default state issues with the ui-sref & URL #948

Closed
CMCDragonkai opened this issue Mar 9, 2014 · 61 comments
Closed

Child default state issues with the ui-sref & URL #948

CMCDragonkai opened this issue Mar 9, 2014 · 61 comments
Assignees
Milestone

Comments

@CMCDragonkai
Copy link

In the FAQ https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#wiki-how-to-set-up-a-defaultindex-child-state

For option 1, you can add a default child state by making the parent state abstract and adding a url to the parent state and allowing the default child state an empty url so it can inherit the parent url and match the parent state's url.

The option 2 is redirection.

$stateProvider.state(
    'parentState',
    {
        abstract: true,
        url: '/parent/child',
        template: '<p>Parent <ui-view /></p>',
        controller: function() {}
    }
).state(
    'parentState.childState',
    {
        url: '',
        template: '<span>Child</span>',
        controller: function(){}
    }
);

However when you go about trying to transition to it such as using ui-sref, it can no longer transition to the parent state anymore. (This is because the parent state is an abstract state).

These things don't work:

<a ui-sref="parentState></a>
OR
$state.go("parentState");

These however work:

<a ui-sref="parentState.childState></a>
$state.go("parentState.childState");

It seems non-intuitive to go about setting up a default childstate and then in the process lose the ability to transition to the parent state. If I could just setup a default childstate, and then transition to the parent state, ui-router should be able to just fallback onto the default child state. I shouldn't need to explicitly set the childstate that I want to go to. What if I want to change what my default child state is? Now all my state declarations need to change!

The option 2 of this child state setup opens up possibility of duplicate content. If you combine option 2 with removing abstract:true from the parent state, and have the child state have its own url segment, then it is a possible solution to the above problem. But surely hardcoding redirection should not be required.

@timkindberg
Copy link
Contributor

I'd wanted this a long time ago, but it kept getting shot down. I agree that we should be able to transition to an abstract state if it has a default child, that's just my opinion though.
See #27

@nateabele
Copy link
Contributor

I'll think about it. Might just be a matter of how to work it into the API cleanly.

@delwyn
Copy link

delwyn commented Apr 24, 2014

Would be great if this could be added.

@goleafs
Copy link

goleafs commented May 5, 2014

This caught me for the past few days... I would definitely endorse a fix.

I did it slightly different, using a redirect. So, to follow the initial example:
$urlRouterProvider.when('/parent', '/parent/child');

ui-sref to 'parent' does not work

HOWEVER
when you manipulate the address bar manually
http://..../parent (which is identical to what shows up as the target for a ui-sref)
it successfully goes to
http://..../parent/child

... pretty inconsistent behavior.

@charandas
Copy link
Contributor

+1 "ability to transition to an abstract state with default child"

@what-is-a-crow
Copy link

+1, for what it's worth. Not being able to do this makes for some gnarly ui-srefs.

@jeffszusz
Copy link

+1

@blah238
Copy link
Contributor

blah238 commented Sep 26, 2014

+1, just got bit by this

christopherthielen referenced this issue in nateabele/ui-router Sep 27, 2014
@12hys
Copy link

12hys commented Oct 21, 2014

+1

Currently trying to use active navigation using "ui-sref-active" with child states. If the parent state is abstract and a nav button, we lose the ability to use ui-sref-active.

@pdwinkel
Copy link

pdwinkel commented Nov 2, 2014

+1

2 similar comments
@fransvelocity
Copy link

+1

@soncodi
Copy link

soncodi commented Nov 6, 2014

+1

@tamakisquare
Copy link

+1 for better consistency in the concept of default child state.

@stocksr
Copy link

stocksr commented Nov 12, 2014

+1
Currently trying to use active navigation using "ui-sref-active" with child states. If the parent state is abstract and a nav button, we lose the ability to use ui-sref-active.

@slessans
Copy link

You can use this to work around this problem (especially the ui-sref-active problem):

$stateProvider.state('parentState', {
    url: '/parent',
    views: {
        '': {
            template: '<p>Parent <ui-view /></p>',
            controller: function() {}
        },
        '@parentState': {
            template: '<span>Child</span>',
            controller: function(){}
        }    
    }
});

Note: the state /parent/child is no just /parent

@iven
Copy link

iven commented Nov 14, 2014

@slessans
Doesn't seem to work:

      .state 'admin.cluster',
        url: '/clusters/{cluster_id}'
        views:
          '':
            template: JST['templates/admin/cluster']
            controller: 'AdminClusterCtrl as cluster'
          '@admin.cluster':
            template: JST['templates/admin/cluster/index']
            controller: 'AdminClusterCtrl as cluster'
      .state 'admin.cluster.master',
        url: '/masters/{master_id}'
        template: JST['templates/admin/cluster/master/index']
        controller: 'AdminMasterCtrl as master'

and

        <a class="list-group-item" ng-repeat="cluster in admin.clusters" ui-sref="admin.cluster({cluster_id: cluster.id})" ui-sref-active="active">

Still ui-sref-active only work in the parent state.

@slessans
Copy link

@iven if ui-sref-active only works for the parent state, make sure you are on the most recent version of ui-router

@iven
Copy link

iven commented Nov 18, 2014

@slessans You're right. I was using 0.2.8.

@bsiddiqui
Copy link

+1

1 similar comment
@chatoo2412
Copy link

+1

@mogelbrod
Copy link

+1, being unable to use ui-sref for abstract parents is really problematic.

@christopherthielen christopherthielen added this to the 1.0.0-preview milestone Nov 27, 2014
@christopherthielen christopherthielen self-assigned this Nov 27, 2014
@harryjoy
Copy link

+1

Just got caught into this.

@mgrinko
Copy link

mgrinko commented Dec 3, 2014

+1

1 similar comment
@chrislambe
Copy link
Contributor

+1

@javialon
Copy link

javialon commented Feb 2, 2015

+1

3 similar comments
@rriemann
Copy link

👍

@raphaelluchini
Copy link

+1

@cykoder
Copy link

cykoder commented Feb 16, 2015

+1

@ermakovich
Copy link

Yes, please make it's possible to use abstract states with ui-sref

@ChrisMondok
Copy link

+1

@eddiemonge
Copy link
Contributor

Lots of interest in seeing this done. @nateabele @timkindberg is this a feature that you are still willing to accept a PR for?

@christopherthielen
Copy link
Contributor

For now, don't declare your state abstract, and use this recipe:


 app.run($rootScope, $state) {
    $rootScope.$on('$stateChangeStart', function(evt, to, params) {
      if (to.redirectTo) {
        evt.preventDefault();
        $state.go(to.redirectTo, params)
      }
    });
  }

  $stateProvider.state('parent' , {
      url: "/parent",
      templateUrl: "parent.html",
      redirectTo: 'parent.child'
  });
  $stateProvider.state('parent.child' , {
      url: "/child",
      templateUrl: "child.html"
  });

@eddiemonge
Copy link
Contributor

@christopherthielen that seems to work pretty well. Why not make that part of ui-router?

@christopherthielen
Copy link
Contributor

@eddiemonge something pretty similar will likely make it in

See #1235

@wefarrell
Copy link

+1

@jswhisperer
Copy link

@christopherthielen +1 fixed my issue

@anonomis
Copy link

+1

1 similar comment
@ghost
Copy link

ghost commented Nov 25, 2015

+1

@SpeedShifter
Copy link

+1

@alexhoo
Copy link

alexhoo commented Jan 5, 2016

+1 (@christopherthielen pretty clever )

@UrVerySpecial
Copy link

+1
thanks a lot!

@mattobee
Copy link

mattobee commented Mar 3, 2016

+1

2 similar comments
@fynnfeldpausch
Copy link

+1

@invernizzie
Copy link

+1

@diegogurgel
Copy link

@christopherthielen Thanks 👍

@christopherthielen christopherthielen modified the milestones: 1.0.0-beta, 1.0.0-alpha.1 Mar 27, 2016
@amazingandyyy
Copy link

+11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests