How can I validate Route params in Angular2? For example, I have a route:
{path: 'artist/:id', component: ArtistInfoComponent}
All I want is to check if parameter id
is a valid number and if so, just show the component. Otherwise show a page with error message.
I found Route Guards in Angular 2 but I think this isn't the best way to do validation because I can have many different params for different pages and writing individual Route Guard for all these pages isn't what I want.
Is there a possibility to specify a regex for each route parameter?
UPD: Maybe there is a way to do a validation on query params? I mean if I have such url: /search?q=blablabla&page=2
how can I check if the parameter page
is valid? Is there a way to do it without Guards?
Guards are the way to go, you could put validation configuration into data
section of the route and create reusable guard to validate parameters based on this configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With