Tuhin Subhra Mandal Tuhin Subhra Mandal - 3 months ago 10
AngularJS Question

AngularJS UI-Router optional url segment in the middle

I want to have an optional url segment for the following example:

url: "/post/:category/:subcategory/:title/{id:int}",


In the above example subcategory is optional. For example it will accept:

url: "/post/sports/football/some-title/10",


and it will also accept:

url: "/post/sports/some-title/15",


which do not have subcategory. I can do that using to separate states but is there any rule for that?
Please note only subcategory segment is optional. Others are mandatory.

Answer

.state('post', { url: '/post/:category/:subcategory/:title/:{id:int}', templateUrl: 'views/post.html', controller: 'postCtrl', params: { subcategory: { squash: true, value: null }, } })

For more info read the doc