Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 681
New Dev Avatar asked Dec 08 '14 11:12

New Dev


1 Answers

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
    }
  }
})
like image 85
Chris T Avatar answered Oct 21 '22 11:10

Chris T