Armin kheirkhahan
Armin kheirkhahan

Reputation: 291

how can use optional url in angular route with ui-router?

I want to define some state with optional url in angular ui-router

how can I do this?

for example

state('test',{
   url:'/list/:id/color?/:color?'
}

in this example the color and :color is optional and state should work whit these urls:

site.com/list/1

site.com/list/color/red

Upvotes: 0

Views: 37

Answers (1)

Michael Doye
Michael Doye

Reputation: 8171

There was an update to the library a few years ago which allowed this functionality:

$stateProvider.state("foo", {
  url: "/foo/{foo}",
  params: {
    foo: { value: "bar" }
  }
});

Adding a default value to foo means that the route above will match /foo, /foo/, and /foo/baz. Additionally, when matching /foo or /foo/, $stateParams will still be populated with { foo: "bar" }

Upvotes: 1

Related Questions