With defaulted parameters how do I rewrite the url?

六月ゝ 毕业季﹏ 提交于 2019-12-13 01:08:56

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/28679820/with-defaulted-parameters-how-do-i-rewrite-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!