How to scroll to top on every Route click in Angular5 ? Last Updated : 06 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report We can use Router and NavigationEnd from '@angular/router' and hence we can scroll to the top of the webpage for every route.Approach:First, we need to import the Router and NavigationEnd from '@angular/router' in both app.module.ts file and app.component.ts.Then we need to create an instance of those in the constructor function.After creating the instance we need to use them in the ngOninit() life cycle hook.In the ngOninit() hook, we need to subscribe to the events of the router and check whether it is an instance of NavigationEnd or not.Then after checking we can use the window.scrollTo() function with (0,0) coordinates to navigate to the top.After following the above steps start your project using the below command.ng serve --openBelow is the implementation of the above steps:app.module.ts: JavaScript import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RouterModule, Routes } from '@angular/router'; import { AppComponent } from './app.component'; const routes: Routes = [ { path: '', component: AppComponent }, ]; @NgModule({ imports: [ BrowserModule, RouterModule.forRoot(routes) ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { } app.component.ts: JavaScript import { Component, OnInit } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html' }) export class AppComponent implements OnInit { constructor(private router: Router) { } ngOnInit() { this.router.events.subscribe((event) => { if (!(event instanceof NavigationEnd)) { return; } window.scrollTo(0, 0) }); } } app.component.html: HTML <link href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"> <div class="jumbotron"> <h1 class="display-4">Hello,Geek!</h1> <p class="lead"> GeeksForGeeks is a website which is a one stop destination for all the computer science related doubts. </p> <hr class="my-4"> <p> Click on the below button to starting learning. </p> <p class="lead"> <a class="btn btn-primary btn-lg" href="#" role="button"> Explore </a> </p> </div> <div class="card"> <div class="card-header"> Featured </div> <div class="card-body"> <h5 class="card-title"> Front End Technologies </h5> <p class="card-text"> HTML, CSS, Javascript, Angular, React.js </p> <a href="#" class="btn btn-primary"> Start Learning </a> </div> </div> <br> <div class="card"> <div class="card-header"> Featured </div> <div class="card-body"> <h5 class="card-title"> Backend Technologies </h5> <p class="card-text"> Node.js, Django,Express </p> <a href="#" class="btn btn-primary"> Start Learning </a> </div> </div> Output: Comment More infoAdvertise with us Next Article How to Scroll to an Element on click in Angular ? B bunnyram19 Follow Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Similar Reads How to Scroll to an Element on click in Angular ? In this article, we will see how to scroll to an element on click in Angular. Here, we will create a component that enables scrolling to specific targets when a button is pressed within the document from one target to another.Steps for Installing & Configuring the Angular ApplicationStep 1: Crea 4 min read How to Create a Scroll To Top Button in React JS ? You will see there are lots of websites, that are using a useful feature like if you're scrolling the webpage, and now you are at the bottom of that page then you can use this button to scroll up automatically like skip to the content. The following example covers create a Scroll To Top button in Re 3 min read How to create module with Routing in Angular 9 ? Angular applications are modular, and NgModules is Angular's own modular architecture. NgModules are containers for closely integrated application domains, workflows, or feature sets that comprise cohesive code blocks. Their scope is governed by the NgModule they include, and they can contain compon 4 min read How To Prevent Scrolling With Navigation in Angular Component? When navigating between routes in an Angular application, the browser's scroll position is often reset to the top of the page. This can be frustrating for users who want to maintain their scroll position between route changes. In this article, we'll explore how to prevent scrolling with navigation i 10 min read How To Scroll To An HTML Element In NextJS? NextJS has various features that enable smooth scrolling to HTML elements. One method involves using useRef to create a reference to the target element and scrollIntoView to achieve smooth scrolling. Another method, using useEffect and window.scrollBy, sets up an event listener on a button for smoot 3 min read How to detect a route change in AngularJS ? In this article, we will see how to detect a route change in AngularJS. In order to detect route change at any moment in AngularJS, this can be achieved by using the $on() method. The $on() method is an event handler, the event which will handle $routeChangeSuccess which gets triggered when route/vi 2 min read Like