0

I have two states like this:

    $stateProvider.state('app', {
        url: '/app',
        template: '<app-index></app-index>',
        data: {},
        resolve: {}
    });
    $stateProvider.state('app.list', {
        url: '/list/:type?',
        template: '<app-list></app-list>',
        data: {},
        resolve: {}
    })

it work fine but when I type url /app/list it redirect to home page, how can I make slash optional? I've tried to put this url '/list/?:type?' but that didn't work. Should I add redirect, if yes then how?

2
  • On a side note I highly approve of your use of the template to load self contained directives instead of specifying the controller and in templateURL for the route. IMHO it makes for much cleaner and more manageable routes. Commented Oct 4, 2016 at 14:45
  • @Enzey I'm using angular 1.5 components. Commented Oct 4, 2016 at 14:50

1 Answer 1

1

I do not believe there property to do this but you can make it work by setting up a subroute with the slash

.state('app', {
    url: '/app',
    template: '<app-index></app-index>',
})
.state('app.withSlash', {
    url: '/',
})

To add additional routes on top of 'app' you can just ignore the existence of the '.withSlash'

.state('app.withOtherParam', {
    url: '/:id',
})

Here is an example of a route where the slash is optional and has an additional param after.

.state('app', {
    url: '/app',
    abstract: true,
    template: '<ui-view/>',
})
.state('app.specificPage', {
    url: '/',
    template: '<someTemplate>'
})
.state('app.withId', {
    url: '/:id',
    template: '<someTemplate>',
})
.state('app.withId.withSlash', {
    url: '/',
})
Sign up to request clarification or add additional context in comments.

1 Comment

It work but I needed .state('app.without', { url: '/list', template: '<app-list></app-list>' });

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.