0

I have the below states, which I would like to map to the same routes, which would then have a parameter.

/en/live/
/en/


/en/live/football
/en/football

Basically, the 'live' part needs to be stored in some kind of variable. I've tried something like:

$stateProvider.state('language.live-lobby', {
    url: "/en/{liveStatus:live|}/football"
}

However, it does not let you specify an empty parameter. Basically, with the above state, /en/live/football matches while /en/football doesn't. If that worked, I could then read the liveStatus parameter.

Is it possible, without having to define multiple states? I would like to avoid having to create multiple states, as they all share the same information like views, data & resolve?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Karl Cassar
  • 6,043
  • 10
  • 47
  • 84

1 Answers1

0

There is a working plunker

Based on these:

we can use this state def:

.state('language.live-lobby', {
  templateUrl: 'tpl.html',
  //url: "/en/{liveStatus:live|}/football",
  url: "/{lang:(?:en|cz|de)}" +
       "/{liveStatus:(?:live|podcast)}" +
       "/{sport:(?:football|golf)}",
  params: {
    lang:       { squash: true, value: 'en' },
    liveStatus: { squash: true, value: 'live' },
    sport:      { squash: true, value: 'football' },
  }
})

All these links would work as expected:

  // these follow defaults
  <a href="#/en/live/">
  <a href="#/en/">
  <a href="#/en/live/football">
  <a href="#/en/football">

  //here we pass some non default
  <a href="#/cz/podcast/">
  <a href="#/cz/">
  <a href="#/cz/podcast/golf">
  <a href="#/cz/golf">

Check it here

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • The Plunker works, however strangely enough for my code it is not working. When it is not left blank, the route matches. When it is left blank, the ui-router matches no state. I'm trying to determine what could be different. – Karl Cassar Nov 03 '15 at 10:32
  • Sad... if you could adjust my plunker to "break" it .. I am ready to assist, if I could... but now I do not know what to add – Radim Köhler Nov 03 '15 at 10:34
  • If I manage to determine what is breaking it, I would. Is there any official documentation on the 'squash' parameter? – Karl Cassar Nov 03 '15 at 13:00