2

This is the configuration i'm using for my ui-router:

.state('parent', {
    url: '/child1'
    controller: ParentController,
    abstract: true
})

.state('parent.child1', {
    url: ''
})

.state('parent.child2', {
    url: '/child2'
})

What I want to acheive ?

I want a /child1 url that I wanted to be default so I made the parent as abstract with child state url as 'empty'.

So I want that when user visits child2 state, the parent url gets replaced rather than appending it. How can i achieve that ?

1 Answer 1

5

If I understand you right, (please correct me if I'm wrong) you are looking to setup a routing service to achieve the following routes:

/, /child1 and /child2 and you want all states to be bound with ParentController

If that is correct you can achieve this with the following:

.state('parent', {
  url: '/',
  controller: ParentController,
})

.state('parent.child1', {
  url: '^/child1'
})

.state('parent.child2', {
  url: '^/child2'
})

For example if we were running https://localhost:3000 we could navigate as such:

parent or / would give us https://localhost:3000/

parent.child1 or /child1 would give us https://localhost:3000/child1

parent.child2 or /child2 would give us https://localhost:3000/child2

The ParentController would be invoked by all states.

Sign up to request clarification or add additional context in comments.

3 Comments

I did the same pattern before but I didn't want the '/' to be visited since it has to have a child.. So making that abstract along with your solution solved it for me.. Thanks.. :) Also didn't have to put ^ in child states
Ah excellent, yes I've had these issues before. UIRouter is so flexible that sometimes there's too many ways to achieve the same result. Glad to help.
Just a sidebar here: the ^ denotes an absolute path. So if a person had two child states like: parent.child.grandChild, and used ^ the url would change from: /parent/child/grandChild/ to /grandChild/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.