8

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

New Dev
  • 48,427
  • 12
  • 87
  • 129

1 Answers1

15

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
    }
  }
})
abyx
  • 69,862
  • 18
  • 95
  • 117
Chris T
  • 8,186
  • 2
  • 29
  • 39
  • Thanks. I tried that, but it only works on first state change, but on subsequent change it shows the year again. [See here](http://plnkr.co/edit/XEqvME1CjQ2x7yAg9HXU?p=preview) - click on one button, then another. Any ideas? – New Dev Dec 09 '14 at 05:34
  • This is really nice solution. The `UI-Router` starts to deliver lot of settings, that's simply awesome. @NewDev just a point of view: *your requirement seems to be **"question for a question"**. Imagine that today someone will share or bookmark your address without year... what a suprise after month, that it does not target what it did... If this requirement comes from some business users... do your best to stop it.* – Radim Köhler Dec 09 '14 at 05:44
  • @RadimKöhler, it's far more important to have a URL to the ongoing (current) view, and it's a very rare case that a different and explicit year is needed, so the requirement to keep the URL simpler. Still, any idea why ChrisT's solution doesn't fully work? – New Dev Dec 09 '14 at 05:46
  • We can argue (and I promise I will stop here ;) But the fact is, that users do not mind about URL. They even do not know what is adress bar (more of them). The point is, **DO NOT create URL which is not UNIQUE RESOURCE Location** ... it is breaking the basic rule. It should not be dependent on the year I am coming to it. At least, promise, that you will remember me later ;) (I promise that this is my last comment here ;) *(PS: in my eyes Chris solution - UI-Router team member - is working. from my perspective)* – Radim Köhler Dec 09 '14 at 05:48
  • Your plunk uses an old release of UI-Router. I updated it for you to 0.2.13: http://plnkr.co/edit/pvdlanklznyUtu4Y4mcS?p=preview I agree with @RadimKöhler that generally the URL should be unique, but it is up to the developer to choose to do so. In your case, you are choosing that in the absence of a year in the URL, you display the current year's stats. Note that 'squash' setting can also squash to some semantic content, such as 'thisyear' or '-'. – Chris T Dec 09 '14 at 14:12
  • Excellent! Thanks - it works. I agree with @RadimKöhler as well, but in this case it's warranted. – New Dev Dec 09 '14 at 18:57