xoxel xoxel - 1 month ago 7
HTTP Question

Add optionnal parameters in my URL with Angular routing

Here is my link:

http://www.example.com/book/details:id/:type


Where "id" and "type" are custom variables. (i know how to build the link there is no problem with that)
And I want the "type" parameter to be optionnal, i've seen some example on the internet like "id:42?/:type" but that doesnt work...

Also my routing is handeling my link this way:

.when('/book/detail:id/:type', {templateUrl : 'partials/detailBook.html'})


Then I use AngularJS's $routeParamaters to get the parameters like in any other of my links.

Thanks for your help.

Answer

Your route should have :type? to make type as optional.

`when('/book/detail/:id/:type?', {templateUrl : 'partials/detailBook.html'})`

The confuguration can be something like,

app.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
     `when('/book/detail/:id/:type?', {templateUrl : 'partials/detailBook.html'})`
}]);