In my app I have a category page that has links to a various product list pages. If it turns out that when you get to a product list page there is only one product then it automatically navigates to that product detail page. What I want is to remove the product list page route in the history so that when the user is on the product detail page and hits the back button they will go to the category page and not the product list page because that will just redirect them back to the detail page.
Asked
Active
Viewed 4.2k times
2 Answers
116
You can use angular routers replaceUrl
flag to do this. See api docs for more details here
this.router.navigate(['/view'], { replaceUrl: true });

alt255
- 3,396
- 2
- 14
- 20
-
2Although I knew about this option, apparently I didn't understand exactly how it worked. – HisDivineShadow Jul 24 '18 at 18:31
-
36It replaces only current state. What about all history? – Gleb Apr 30 '19 at 21:49
-
1Worth noting that only the current active route is replaced with the new navigation route in the history paths, all other history persists. – Ralpharoo Jan 21 '20 at 00:09
-
I feel so stupid right, now the docs say literally "When true, navigates while replacing the current state in history" and I'm wondering how to handle the case :D – Християн Христов Aug 09 '22 at 10:41
-6
If using the $location object in order to switch views, a way of doing the same as in @alt255 's post is by using the replace() method after calling path("..."):
$location.path("/").replace();

Victor
- 1,001
- 2
- 14
- 25
-
1There is no such thing as `$location` in Angular, that's an AngularJs service. – HisDivineShadow Feb 28 '19 at 14:57
-
The '$location' solution is indeed for AngularJS. Though, a good solution for the AngularJS scenario. – Victor Mar 01 '19 at 11:00
-
5