使用默认参数如何重写 url? [英] With defaulted parameters how do I rewrite the url?

查看:21
本文介绍了使用默认参数如何重写 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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',
    })

这有效,控制器中的参数确实是今天的日期,但 url 仍然为 http://local/some/report/ 我该如何更改它以使 url 变为 :: <代码>http://local/some/report/2015-02-23

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

推荐答案

我想说,你的状态定义应该有效,如图所示这个工作示例

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

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

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',
    });

更多关于这个的文档:API 参考 $stateProvider

More about this in doc: API reference $stateProvider

squash - {bool|string=}: 当当前参数值与默认值相同时,squash 配置默认参数值在 URL 中的表示方式.如果未设置壁球,则使用配置的默认壁球策略.(参见 defaultSquashPolicy())

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())

有三种壁球设置:

  • false:参数的默认值没有被压缩.它被编码并包含在 URL 中
  • true:URL 中省略了参数的默认值.如果参数在状态的 url 声明中前后都有斜杠,则省略其中一个斜杠.这可以让网址看起来更简洁.
  • "":参数的默认值替换为您选择的任意占位符.
  • 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.

此处查看

这篇关于使用默认参数如何重写 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆