0
votes

I am trying to define a single state with an optional integer route parameter.

I have defined the following state in my application

$stateProvider.state('configuration', {
  stateUrl: '/configuration/{id:int}',
  template: '<div></div>'
});

This is working for all routes that have an id e.g. /configuration/1, but it does not work for routes without the id e.g. /configuration/.

If I drop the type definition, like the below, then routes work with or without the id at the end but I end up with a string id rather than a number.

$stateProvider.state('configuration', {
  stateUrl: '/configuration/:id',
  template: '<div></div>'
});

Is it possible to define an optional typed route parameter?

2

2 Answers

1
votes

I managed to get this working by using the params option.

$stateProvider.state('configuration', {
  stateUrl: '/configuration/{id:int}',
  params: { id: { value: null, squash: true } },
  template: '<div></div>'
});
0
votes

You can make parameter optional using '?'

Please try following

$stateProvider.state('configuration', {
    stateUrl: '/configuration/:id?',
    template: '<div></div>'
});