I have an app of which one can navigate to a page in which there is no way forwards. Think of clicking a program in a TV guide to open a page with that program's details, obviously you will want to go back to the TV guide.
For a button to go backward to the TV guide, I have an <a>
tag.
Which would be the most advisable use case for this:
- Using
href="/guide"
or
- Using
(click)=goBack()
where the function callslocation.back()
I have an app of which one can navigate to a page in which there is no way forwards. Think of clicking a program in a TV guide to open a page with that program's details, obviously you will want to go back to the TV guide.
For a button to go backward to the TV guide, I have an <a>
tag.
Which would be the most advisable use case for this:
- Using
href="/guide"
or
- Using
(click)=goBack()
where the function callslocation.back()
- I would use the "back" method from the location service imported with: import {Location} from '@angular/mon'; – Geku Commented Aug 23, 2018 at 10:59
- you can get answer from this link stackoverflow./questions/35446955/how-to-go-back-last-page/… – mittal bhatt Commented Aug 23, 2018 at 11:00
- 1 Possible duplicate of How to go back last page – NullPointer Commented Aug 23, 2018 at 11:03
- Consider if the user reaches the page by another way. If they click a Google search result that goes directly to the program details page, should the back button take them to the guide page, or back to their Google search results? – Joe Daley Commented Nov 2, 2019 at 6:46
2 Answers
Reset to default 3You should use built-in Location service of Angular as.
Angular-Location
import {Component} from '@angular/core';
import {Location} from '@angular/mon';
@Component({
selector: 'app-root',
templateUrl: './app.ponent.html',
styleUrls: ['./app.ponent.scss']
})
class AppComponent {
constructor(private location: Location) {
}
back() {
this.location.back();
}
}
If you want to navigate in an Angular app, you don't want to use classic href
links since they are designed to work for server-rendered apps, but not client-side apps that use the History Javascript API.
Prefer using the RouterLink from Angular.
Between RouterLink
and location.back()
the choice is yours, it depends whether you want to control the page you want to redirect to, or just want the same behavior as the back button from your browser.