1

When using nested states from UI Router, this will only work if you navigate the page through the nest. i.e.:

If I have

.state('home', {
    url: '/',
    templateUrl: 'home.html'
    ...
})
.state('home.page', {
    url: '/page',
    templateUrl: 'page.html'
    ...
})
.state('home.page.sub' , {
    url: '/page/sub',
    templateUrl: 'sub.html'
    ...
})

Now the problem is if I directly visit /page/sub without going to / and then click on link to go to /page and finally navigate to /page/sub, then the linking is not proper and the elements of the page will not fully load.

How can I fix this linkage issue?

1 Answer 1

1

Almost the same issue could be found here:

I've created plunker showing a working example. The trick here is to use one of the ui-router features:

As we can observe in the plunker/code snippet below, we have to adjust the state definition. We need to be able to distinguish among states just by the passed url route

1) first level, a home state, will be used for path '/'
2) second level, a page state, won't be using the parent url, but will be defined as '/page' ... starting from the root.

Therefore ui-router will be able to decide which state to issue. Without the Absolute Routes (^) we would need combine parent and child: '//page' would be the proper route...

.state('home', {
    url: '/',
    templateUrl: 'home.html'
    ...
})
// here is the change, see the ^
.state('home.page', {
    url: '^/page',
    templateUrl: 'page.html'
    ...
})
.state('home.page.sub' , {
    url: '/page/sub',
    templateUrl: 'sub.html'
    ...
})

And now, each state could be accessed as

<li><a ui-sref="home">Home</a></li>
<li><a ui-sref="home.page">- Page</a></li>
<li><a ui-sref="home.page.sub">- - Sub</a></li>

And its url will be:

#/          -- this is home
#/page      -- the page, in fact starting from root
#/page/sub

Working plunker: plunker

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

1 Comment

Great to see that. The ui-router is ... awesome ;)

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.