0

I'm trying to understand how can I limit the options for a param in a route in Angular.

When I'm declaring a route:

{
  path: 'food/:name',
  component: FoodComponent,
},

:name can be anything. What I'd like to do is to limit the options to: apple or orange, etc.. and if the value is other than the available options - catch it as the 404 route (path: '**').

I guess I can always verify the param at the component level, and redirect to 404 if the value isn't match.. but I thought maybe is there is a way to do that at the route level.

Thanks in advance!

ET-CS
  • 6,334
  • 5
  • 43
  • 73
  • Did you try resolver? – Ali Jan 18 '20 at 11:30
  • 1
    You can use url matcher as mentioned [here](https://stackoverflow.com/questions/42002221/using-regex-for-path-value-of-routes-in-angular2) – Eldar Jan 18 '20 at 11:38
  • You can use a `guard` for this. But it looks like you can create separate routes `food/apple` and `food/orange`. With this angular router will redirect to the 404 route if somebody tries to open something else. – epsilon Jan 18 '20 at 12:09
  • @epsilon There could be a lot of available options, so create a route for each might be repetitive. – ET-CS Jan 18 '20 at 12:18
  • Thank you @Eldar. exactly what I was looking for! – ET-CS Jan 18 '20 at 12:19

0 Answers0