Pranav Nutalapati
Pranav Nutalapati

Reputation: 734

How to Dynamically Show 404 in Angular

I searched over and over again, but maybe my search terms were incorrect, because nothing turned up. I want to dynamically show my 404 component depending on the results of an HTTP request.

I realize that I can do this by redirect the user to my 404 page, but is there any way I can render the component without changing the URL?

Something like /people/<nonexistent id>

  1. Perform HTTP request
  2. If person exists, proceed as normal
  3. If person doesn't exist, render 404 without changing the URL

Thank you!

Upvotes: 4

Views: 872

Answers (1)

Pranav Nutalapati
Pranav Nutalapati

Reputation: 734

Never mind, I found the answer.

The solution is to userouter.navigate with the option skipLocationChange set to true. This tells Angular to reroute without changing the URL in the browser navbar.

https://angular.io/api/router/NavigationExtras#skipLocationChange

Upvotes: 10

Related Questions