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 3.4k times
2 Answers
101
You can use angular routers replaceUrl flag to do this. See api docs for more details here
this.router.navigate(['/view'], { replaceUrl: true });
alt255
- 2,986
- 2
- 13
- 19
-
2Although I knew about this option, apparently I didn't understand exactly how it worked. – HisDivineShadow Jul 24 '18 at 18:31
-
32It 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
-3
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
- 807
- 2
- 11
- 23
-
There 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