1

I have a route that has an optional parameter of a date in the format YYYY-MM-DD. If a date is not supplied, I will supply today's date. I defined my route like so:

  .state('some.report', {
    url: '/report/:reportDate',
    templateUrl: 'app/report/report.html',
    params:{reportDate:moment().format('YYYY-MM-DD')},
    controller: 'reportCtrl',
    })

This works, the parameter in the controller is indeed today's date, but the url remains as http://local/some/report/ How can I change it so that url becomes:: http://local/some/report/2015-02-23

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Michael B
  • 7,512
  • 3
  • 31
  • 57

1 Answers1

3

I would say, that your state definition should work, as shows this working example

We can also force it with a setting squash: false

    .state('some_report', {
      url: '/report/:reportDate',
      templateUrl: 'tpl.html',
      params: {
        reportDate: {
          value: moment().format('yyyy-MM-dd'),
          squash: false,
        }
      },
      controller: 'reportCtrl',
    });

More about this in doc: API reference $stateProvider

squash - {bool|string=}: squash configures how a default parameter value is represented in the URL when the current parameter value is the same as the default value. If squash is not set, it uses the configured default squash policy. (See defaultSquashPolicy())

There are three squash settings:

  • false: The parameter's default value is not squashed. It is encoded and included in the URL
  • true: The parameter's default value is omitted from the URL. If the parameter is preceeded and followed by slashes in the state's url declaration, then one of those slashes are omitted. This can allow for cleaner looking URLs.
  • "<arbitrary string>": The parameter's default value is replaced with an arbitrary placeholder of your choice.

Check it here

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335