How to implement history.back() in AngularJS?
Last Updated :
30 Jul, 2024
Angular JS is a typescript-based web application framework. It is supported by the Angular team of Google and open-source developers. It is a component-based framework allowing the developers to reuse the components created. It is well-suited for large and complex applications because of its well-defined project structure, templates, etc. In this article, we will see how to use the history.back() method in AngularJS, along with understanding it with the help of a suitable example.
The history.back() Method is one of the most common operations while navigating through URLs where it allows the developers to programmatically go to the previous page. It is provided with inbuilt HTML.
Implementing the history.back() using $window service
Window service is none other than a global window object inside the wrapper. It provides the browser-specific functionality and hence using the same, we can implement the history.back().
Syntax
window.history.back();
We will create a directive and then create a function that calls the above method. We can then use the function in a button which on pressed navigates back. This is an easy process since we do need not to import any package or library. Hence it provides no overhead since the window object is accessible globally.
Configuring the Angular Application
Step 1: Create a new project
ng new history_back_gfg
Step 2: Add routing to project
ng generate module app-routing --flat --module=app
Step 3: Create a new component, called Home and About
ng generate component about
ng generate component home
Project Structure:

Step 4: Modify the src>app>app.component.html as follows:
HTML
<div>
<h1 style="color: green;
text-align: center;">
Welcome to GeeksforGeeks
</h1>
<h3 style="text-align: center;">
How to implement history.back() in AngularJS?
</h3>
<router-outlet></router-outlet>
</div>
Step 5: Modify the src>app>home>home.component.html
JavaScript
<div>
<h3>This is Home page</h3>
<a routerLink="/about">About</a>
</div>
Step 6: Modify the src>app>about>about.component.html
HTML
<div>
<h3>This is About page</h3>
<button appGoBack>Go Back</button>
</div>
Step 7: Modify src>app>app-routing.module.ts as follows:
JavaScript
import { NgModule } from '@angular/core';
import { RouterModule, Routes }
from '@angular/router';
import { AppComponent } from './app.component';
import { AboutComponent }
from './about/about.component';
import { HomeComponent }
from './home/home.component';
const routes: Routes = [
{
path: '',
component: HomeComponent,
},
{
path: 'about',
component: AboutComponent,
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Step 8: Create a directive as follows for the back press:
ng generate directive go-back
Here, in the directive, we will override the HostListener onClick event so that the onClick of button navigates back. Here is the src>app>go-back.directive.ts
JavaScript
import { Directive, HostListener }
from '@angular/core';
@Directive({
selector: '[appGoBack]'
})
export class GoBackDirective {
constructor() { }
@HostListener('click', ['$event'])
onClick(event: MouseEvent) {
event.preventDefault();
window.history.back();
}
}
Step 9: Run the application
ng serve --open
Output:
Similar Reads
How To Implement CanDeactivateFn in Angular15? The CanDeactivateFn is a function in Angular that allows you to control whether a route can be deactivated or not. It's often used to prevent users from navigating away from a page with unsaved changes. In this article, we will see how to implement CanDeactivateFn in Angular15.ApproachUsing a functi
3 min read
How to Sort List by Date Filter in AngularJS ? AngularJS is a feature-based JavaScript framework that uses various tools to create dynamic single-page web applications. While developing the application we have the requirement to play with the data in AngularJS and sort this list of data by a date properly in descending order. This can be done us
5 min read
How to access cookies in AngularJS? AngularJS is the most useful framework for developing dynamic web applications. While developing the applications, we need to store and access the cookies for many other functionalities like retiring the user details, etc. Cookies are nothing but the data that is usually stored in the client's brows
5 min read
How to set, get and clear cookies in AngularJS ? In this article, we will see how to set, get & clear the Cookies in AngularJS, along with understanding the basic usage through the implementation. In AngularJS, we need to use angular-cookies.js to set, get and clear the cookies. You can use the live CDN link for this: https://cdnjs.cloudflare.
2 min read
How to Handle Anchor Hash Linking in AngularJS ? AngularJS Anchor Hash Linking consists of developing a proper and efficient navigation experience in the one-page application. This feature of AngularJS enables users and developers to properly and smoothly scroll to specific sections of a webpage just by clicking on the navigation links, rather tha
5 min read