How to scroll to top on every Route click in Angular5 ? Last Updated : 06 Aug, 2024 Comments Improve Suggest changes 1 Likes 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: Create Quiz Comment B bunnyram19 Follow 1 Improve B bunnyram19 Follow 1 Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Explore AngularJS BasicsAngularJS Tutorial 5 min read Introduction to AngularJS 4 min read Angular CLI | Angular Project Setup 3 min read AngularJS Expressions 2 min read AngularJS Modules 3 min read AngularJS ng-model Directive 4 min read AngularJS Data Binding 4 min read AngularJS Controllers 3 min read AngularJS | Scope 2 min read AngularJS Services 4 min read AngularJS | AJAX - $http 3 min read AngularJS | Tables 2 min read AngularJS Select Boxes 2 min read AngularJS SQL 3 min read AngularJS HTML DOM 2 min read AngularJS Events 3 min read AngularJS | Forms 3 min read AngularJS Form Validation 3 min read AngularJS | API 2 min read AngularJS and W3.CSS 2 min read AngularJS Includes 3 min read AngularJS Animations 1 min read AngularJS | Application 3 min read AngularJS DirectivesAngularJS Directives 9 min read AngularJS ng-app Directive 1 min read AngularJS ng-bind Directive 2 min read AngularJS ng-bind-html Directive 2 min read AngularJS ng-bind-template Directive 2 min read AngularJS ng-blur Directive 1 min read AngularJS ng-change Directive 2 min read AngularJS ng-checked Directive 2 min read AngularJS ng-class Directive 2 min read AngularJS ng-class-even Directive 2 min read AngularJS ng-class-odd Directive 2 min read AngularJS ng-click Directive 2 min read AngularJS ng-cloak Directive 2 min read AngularJS ng-controller Directive 2 min read AngularJS Directives Complete Reference 2 min read AngularJS FiltersAngularJS | Filters 7 min read AngularJS currency Filter 2 min read AngularJS | date Filter 2 min read AngularJS filter Filter 3 min read AngularJS json Filter 2 min read AngularJS limitTo Filter 2 min read AngularJS lowercase Filter 1 min read AngularJS number Filter 1 min read AngularJS orderBy Filter 4 min read AngularJs uppercase Filter 1 min read AngularJS Converting FunctionsAngularJS angular.lowercase() Function 2 min read AngularJS angular.uppercase() Function 1 min read AngularJS angular.forEach() Function 1 min read AngularJS Comparing FunctionsAngularJS angular.isArray() Function 2 min read AngularJS angular.isDate() Function 2 min read AngularJS angular.isDefined() Function 2 min read AngularJS angular.isElement() Function 2 min read AngularJS angular.isFunction() Function 2 min read AngularJS angular.isNumber() Function 2 min read AngularJS angular.isObject() Function 2 min read AngularJS | angular.isString() Function 1 min read AngularJS angular.isUndefined() Function 2 min read AngularJS angular.equals() Function 2 min read AngularJS angular.toJson() Function 2 min read AngularJS QuestionsHow to bundle an Angular app for production? 4 min read How to add many functions in one ng-click directive? 2 min read How to directly update a field by using ng-click in AngularJS ? 3 min read How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ? 3 min read How to detect when an @Input() value changes in Angular? 3 min read How to open popup using Angular and Bootstrap ? 2 min read How to reload or re-render the entire page using AngularJS? 2 min read How to add input fields dynamically on button click in AngularJS ? 2 min read How to Create Button Dynamically with Click Event in Angular ? 2 min read How to use jQuery in Angular ? 2 min read AngularJS Examples 2 min read Like