angularurlroutesurl-validation

Angular - Verify url parameters on service


Good day,

I have an angular application with the following routes:

orders/123
orders/123/lines
messages/123
messages/123/lines/new`

I have a lot more routes then this and a lot more entities.

I want to verify that the id orders/**:id** is a valid id of an existing order. If so I want to show the page and if not i want to redirect to a general 404 page.

I have a lot of entities so I would like to solve this as generally as possible. What is a good way to approach this?

Thanks in advance!


Solution

  • You can get the id in the ngOnInit event with ActivatedRoute from '@angular/router' and create a method which check if the entity exists serverside.

    If not you can navigate to your 404 page with the Router from '@angular/router':

    this.router.navigate(['/your-404'])