ui.router:如何从URL中省略默认参数

时间:2014-12-08 11:10:30

标签: angularjs angular-ui-router

我使用$stateProvider定义了以下状态:

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

更改一年时,如果网址与默认值匹配(例如2014年),我希望该网址省略网址的{year}部分。换句话说,何时:

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

当我切换到byPlayer视图时(假设年份是2014年 - 默认):

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

否则,网址为:www.example.com/app/#/player/555/2013

1 个答案:

答案 0 :(得分:14)

docs

中阅读params的{​​{3}}和squash
$stateProvider.state("byPlayer", {
  url : "/player/{id}/{year}", 
  params: { 
    year: { 
      value: function() { return getCurrentYear(); },
      squash: true
    }
  }
})