1

I have an angularJS application where the route can be one of the below

www.website.com/blog/xyz
www.website.com/blog/xyz/title/other-params

In last url, title parameter is use for user readability sake, so it's not compulsory in the url. Hence I am using below angularJS code for my route.

$routeProvider
    .when('/blog', {
        templateUrl : url,
        controller : 'blogsHome'
    })
    .when('/blog/:blog_id', {
        templateUrl : url,
        controller : 'blogInfo'
    })
    .when('/blog/:blog_id/:url_params*', {
        templateUrl : url,
        controller : 'blogInfo'
    });

Now, in above code, I had to add middle when because angular expect :url_params and can not be empty. Is there any way I can omit middle when statement?

2

2 Answers 2

1

Adding * to you work with multiple levels of directories dynamically and adding ? to work with optional path. But when you want work with multiple levels but optional then can try like this ?:multipleOptionalParams*?

so try this one:

$routeProvider
    .when('/', {
        templateUrl : 'blog.html',
        controller : 'myCtrl'
    })
    .when('/blog/:blog_id/?:url_params*?', {
        templateUrl : 'details.html',
        controller : 'details'
    });

Can visit Plunker Demo

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

Comments

0

I would like to replace the 3rd when with the second one to change the sequence which when matches first.

Like so, the most complicated first:

$routeProvider
    .when('/blog', {
        templateUrl : url,
        controller : 'blogsHome'
    })
    .when('/blog/:blog_id/:url_params*', {
        templateUrl : url,
        controller : 'blogInfo'
    })
    .when('/blog/:blog_id', {
        templateUrl : url,
        controller : 'blogInfo'
    });

Comments

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.