ui.router: how to omit a default parameter from URL

我只是一个虾纸丫 提交于 2019-12-08 16:45:39

问题


I have the following states defined with $stateProvider:

$stateProvider.state("byTeams", {url : "/team/{id}/{year}", ...})
$stateProvider.state("byPlayer", {url : "/player/{id}/{year}", ...})

When changing a year, I would like the URL to omit the {year} part of the URL if it matches the default (say 2014). In other words, when:

$state.go("byTeams", {year: 2014}) --> www.example.com/app/#/team/343
$state.go("byTeams", {year: 2013}) --> www.example.com/app/#/team/343/2013

And when I switch to a byPlayer view (assuming the year is 2014 - default):

$state.go("byPlayer", {id: 555}) --> www.example.com/app/#/player/555/

Otherwise, the URL would be: www.example.com/app/#/player/555/2013


回答1:


Read the docs for params and squash in $stateProvider.state()

$stateProvider.state("byPlayer", {
  url : "/player/{id}/{year}", 
  params: { 
    year: { 
      value: function() { return getCurrentYear(); },
      squash: true
    }
  }
})


来源:https://stackoverflow.com/questions/27356693/ui-router-how-to-omit-a-default-parameter-from-url

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