使用默认参数如何重写 url?

With defaulted parameters how do I rewrite the url?

我有一条路线,它有一个日期的可选参数,格式为 YYYY-MM-DD。如果没有提供日期,我将提供今天的日期。 我这样定义我的路线:

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

这行得通,控制器中的参数确实是今天的日期,但是 url 仍然是 http://local/some/report/ 我怎样才能改变它,使 url 变成 :: http://local/some/report/2015-02-23

我会说,您的状态定义应该有效,如图所示 this working example

我们也可以通过设置squash: false

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

文档中有更多相关信息: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.

检查一下here