How to scroll to top on every Route click in Angular5 ? Last Updated : 06 Aug, 2024 Comments Improve Suggest changes 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 Must Coding Questions - Company-wise B bunnyram19 Follow Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like