5

Im trying to set up multiple nested states inside my app. This is the related code.

function configFn($stateProvider, $urlRouterProvider) {
        $urlRouterProvider.otherwise("/cart");
        $stateProvider
            .state('checkout', {
                url: '/',
                templateUrl: 'app/checkout/views/checkout.container.html',
                controller: 'checkoutCntrl',
                abstract: true
            })
                .state('checkout.cart', {
                    data: { step: 1 },
                    url: '/cart',
                    views: {
                        "styles": {
                            templateUrl: 'app/checkout/views/checkout.styles.html',
                            controller: 'checkoutStylesCntrl'
                        },
                        "cart": {
                            templateUrl: 'app/checkout/views/checkout.cart.html',
                            controller: 'cartCntrl'
                        }
                    }
                })
                //.other states

    }

the problem that im facing is that the state and transition from states isn't happening.
So if i open my page with nothing after #/ then i get to see nothing.

If I put //cart in the url the app works fine which seems logical as //cart probably means that the first / is for state checkout and /cart after the first / is for the state checkout.cart.

But the problem is that i want state checkout.cart to load on the url /cart and not //cart

2 Answers 2

12

The solution is pretty simple.

As you're using an abstract state. don't give it an URL.

        .state('checkout', {
            templateUrl: 'app/checkout/views/checkout.container.html',
            controller: 'checkoutCntrl',
            abstract: true
        })

This should do the trick.

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

Comments

3

One solution, is to start url evaluation without parent. We can do that with this sign ^

.state('checkout.cart', {
    data: { step: 1 },
    url: '^/cart',
    ...

Absolute Routes (^)

If you want to have absolute url matching, then you need to prefix your url string with a special symbol '^'.

3 Comments

That should work. But in many usecases the common controller from the abstract state is really usefull (for common resolves for exemples). The template added by the abstract state has to be on each view too.
@Okazari The point is, that the absolute routing does not change parent/child hierarchy at all. Everything is in place, abstract parent does its job, e.g. provides some default views. And as I said.. this is just One solution...
Didn't know that it keep everything in place. So it just basically don't use the other parents states URL ? I need to test that on my own to see how it work i guess. Thx for sharing.

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.