New Dev
New Dev

Reputation: 49600

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

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

Upvotes: 8

Views: 7688

Answers (1)

Chris T
Chris T

Reputation: 8216

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

Upvotes: 15

Related Questions