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

Redirect abstract states #497

Closed
blackjid opened this issue Oct 11, 2013 · 22 comments
Closed

Redirect abstract states #497

blackjid opened this issue Oct 11, 2013 · 22 comments

Comments

@blackjid
Copy link

Is there any way to redirect to another state when I try to load an abstract state?

.state('client', {
   url: '/client/:clientId',
   abstract: true
})
.state('client.details' {
   url: '/details',
   templateUrl: '/views/mytemplate.html',
   controller: 'ClientDetailsCtrl'
});

I'd like to redirect to the client.details state whenever I navigate to the url that match the client state

thanks

@blackjid
Copy link
Author

Maybe something like

.state('client', {
   url: '/client/:clientId',
   abstract: true,
   redirectTo: 'client.details'
})

@blackjid
Copy link
Author

I tried with

$urlRouterProvider
      .when('/clients/:clientId', '/clients/:clientId/details')

But it doesn't work..

@randallmeeker
Copy link

Just use $state.go(), or the Wiki has documentation about default child states.

.state('client', {
   url: '/client/:clientId',
   abstract: true,
    controller: ['$scope', '$state', 
        function( $scope, $state) {
            $state.go('client.details');
        }],   
})

or Wiki Default Child States
https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-set-up-a-defaultindex-child-state

@blackjid
Copy link
Author

thanks @nateabele I figure it out, that I had a mistyping in the $urlRouterProvider redirection....

I still have a problem though... given this routing

$stateProvider
    .state('client', {
        url: '/client/:clientId',
        abstract: true
    })
    .state('client.details' {
        url: '/details',
        templateUrl: '/views/mytemplate.html',
        controller: 'ClientDetailsCtrl'
     });
$urlRouterProvider
    .when('/client/:clientId', '/client/:clientId/details')

This is working, when I go to /client/534 I get redirected to /client/534/details

But when I navigate using ui-sref="client" it does the redirection but it triggers a exception

Uncaught Error: Cannot transition to abstract state 'client.details 

Is this by design and I shouldn't be calling an abstract state? I thought that the redirection would happened before the state call...

@command-tab
Copy link

I'm running into what I think is the same issue. I'm using ui-sref to go to an abstract state (like client) and depending on the default child state (client.details) redirection to happen. When clicking the link that the ui-sref generates, the state change and subsequent redirect all work great, but that same error is thrown in the console. It seems as if the error is being thrown before any redirection has a chance to run.

@nateabele
Copy link
Contributor

Is this by design and I shouldn't be calling an abstract state?

Yes, and yes. If the state is always redirecting anyway, just don't make it abstract.

@prateekpronoc
Copy link

If state has child state then only we make it abstract..

As per your use following is the eg.

Eg.
$stateProvider
.state('ParentState', {
url: 'parentState/',
abstract: true,
views: {
'@': {
templateUrl: 'parentDir/parent.html',
controller: 'ParentCtrl as vm'
}
}
})
.state('ParentState.someview', {
url: 'card',
templateUrl: 'ParentDir/parentState.someview.html'
});

so if u want to redirect to this state on some action
$state.go('ParentState.someview');

@rhutchison
Copy link

$stateProvider
    .state('client', {
        url: '/client/:clientId',
        templateUrl: '/views/parent.html', // must have <div ui-view></div>
        controller: 'ClientDetailsCtrl',
        abstract: true
    })
    .state('client.default' {
        url: '',
        templateUrl: '/views/mytemplate.html'
    })
    .state('client.details' {
        url: '/details',
        templateUrl: '/views/mytemplate.html'
    });

@captainkovalsky
Copy link

Some directives used $state.go(parentState); and they didn't know anything about states(abstract or not), so I think redirectTo feature should be implemented in ui-router

@mjrk
Copy link

mjrk commented Apr 9, 2015

+1 for a tested redirectTo feature

@Namek
Copy link

Namek commented Apr 13, 2015

Use DSR from ui-router-extras and configure deepStateRedirect.default like here:
http://christopherthielen.github.io/ui-router-extras/#/dsr And don't make your parent state abstract.

@ghost
Copy link

ghost commented Sep 7, 2015

The lack of a default on abstract states appears to be a glaring omission given its usefulness and the recurring demand for it. Using "when" to redirect is not a direct solution and feels like a hack. What developers are looking for is to $state.go('target') where 'target' resolves to an optional default, if set, given an abstract target. Otherwise we are messing with URLs rather than states as a work-around.

@pduey
Copy link

pduey commented Sep 10, 2015

There is a default on the abstract state, and there are a couple of options for implementing (per the wiki), and they both work except when you use ui-sref. I.e., if you type the url to the abstract state into the browser, it correctly loads the default child state. But, if you click on a link that goes to the abstract state, it's gets the error. Why would the two things behave differently when it's the same URL?

@nateabele
Copy link
Contributor

@pduey That shouldn't happen. Open a new issue and post a plunkr that demonstrates it.

@filipesilva
Copy link
Contributor

I'm having the issue described by @pduey as well. I'll put up a new issue with plunkr.

@filipesilva
Copy link
Contributor

As a note here, having the ui-sref links not work with the FAQ solution also means that ui-sref-active does not work.

@zenpuppet
Copy link

I have been using the ui-router in a tree menu that generates from a JSON object.

I was having a horrible time with this, this being the ui-sref not working and the FAQs saying that it should work. At the end of it all, I created some code that appended .index onto any parents that I wanted to load in an "overview" child page. I will be posting the entire solution up in a couple days. It's a wreck at the moment otherwise i would upload it now. It's fairly useful for roughing together prototypes that need a fairly complex, multi-level tree structure.

@nateabele
Copy link
Contributor

@zenpuppet That sounds like something worth adding to the wiki FAQ.

@zenpuppet
Copy link

I have just uploaded the code. I am doing a bit of polish on it so its slightly more robust. I am fixing my code so you can define if you want a parent to use just the

 <div ui-vew></div> 

as a template or if you actually have a templateUrl. I also want to fix it so that in the parent node you can define a particular child as the default subview. I'm doing that today. If you are interested in seeing the code in its current state let me know.

@nateabele
Copy link
Contributor

@zenpuppet No, that's fine (thanks for asking). Ping when it's polished.

@bernardoadc
Copy link

@zenpuppet have you advanced on this?

@jeremysawesome
Copy link

This solution works and it needs more exposure:
#1431 (comment)

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

No branches or pull requests