Angular ui router route move to top page năm 2024

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 --open

Below 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 { NgModule } from `0`import { NgModule } from `1; `

`import { NgModule } from `3

`import { NgModule } from `4`import { NgModule } from `5`import { NgModule } from `6`import { NgModule } from `7

`import { NgModule } from `8

`import { NgModule } from `9

import { NgModule } from `4'@angular/core'`1

'@angular/core'`2'@angular/core'`3

import { NgModule } from `4'@angular/core'`5

import { NgModule } from `4'@angular/core'`7

`'@angular/core'`8

`'@angular/core'`9

app.component.ts:

Javascript

; `0'@angular/core' ; `

; `3'@angular/router' ; `

`; `6

; `7; 8; `9`import { BrowserModule } from `0

`; `7`import { BrowserModule } from `2`import { BrowserModule } from `3

`'@angular/core'`8

`import { BrowserModule } from `5

`; `7`import { BrowserModule } from `7

`; `7`import { BrowserModule } from `9

'@angular/core'`2'@angular/platform-browser'1'@angular/platform-browser'`2

'@angular/platform-browser'`3'@angular/platform-browser'4 '@angular/platform-browser'5'@angular/platform-browser'6 '@angular/platform-browser'`7

'@angular/platform-browser'`8'@angular/platform-browser'9; `

'@angular/platform-browser'`3; `2

'@angular/platform-browser'`3; `4

'@angular/core'`2; `6

; `7; `2

`; `9

app.component.html:

HTML

`import { RouterModule, Routes } from `0`import { RouterModule, Routes } from `1 `import { RouterModule, Routes } from `2`import { RouterModule, Routes } from `3

`; `7`import { RouterModule, Routes } from `5`import { RouterModule, Routes } from `6`import { RouterModule, Routes } from `7`import { RouterModule, Routes } from `8

import { RouterModule, Routes } from `0'@angular/router'0 '@angular/router'1`import { RouterModule, Routes } from `6'@angular/router'`3`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0'@angular/router'7 '@angular/router'1`import { RouterModule, Routes } from `6; 0; 1'@angular/router'`7`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0; 6 '@angular/router'1`import { RouterModule, Routes } from `6; `9`import { RouterModule, Routes } from `8

`'@angular/core'`2`import { NgModule } from `02

`'@angular/core'`2`import { NgModule } from `04

`'@angular/core'`2`import { NgModule } from `06

`'@angular/core'`2`import { NgModule } from `08

; `7`import { NgModule } from `10; `6`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0`import { NgModule } from `15 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `18`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0; `6`import { RouterModule, Routes } from `8

`'@angular/core'`2`import { NgModule } from `25

`'@angular/core'`2`import { NgModule } from `27

; `7`import { NgModule } from `10; `6`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0; 6 '@angular/router'1`import { RouterModule, Routes } from `6; `9`import { RouterModule, Routes } from `8

'@angular/core'`2`import { RouterModule, Routes } from `0`import { NgModule } from `41 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `44

`'@angular/platform-browser'`3`import { RouterModule, Routes } from `2`import { RouterModule, Routes } from `6`import { NgModule } from `48 `import { NgModule } from `49`import { RouterModule, Routes } from `6`import { NgModule } from `51`import { RouterModule, Routes } from `8

`'@angular/platform-browser'`3`import { NgModule } from `54

`'@angular/core'`2`import { NgModule } from `10`import { NgModule } from `41`import { RouterModule, Routes } from `8

; `7`import { NgModule } from `10; `6`import { RouterModule, Routes } from `8

import { NgModule } from `10'@angular/router'`0`import { RouterModule, Routes } from `8

import { RouterModule, Routes } from `0'@angular/router'0 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `70`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0'@angular/router'0 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `77`import { RouterModule, Routes } from `8

`'@angular/core'`2`import { NgModule } from `80

; `7`import { NgModule } from `10'@angular/router'`0`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0'@angular/router'0 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `90`import { RouterModule, Routes } from `8

'@angular/core'`2`import { RouterModule, Routes } from `0`import { NgModule } from `94 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `97`import { RouterModule, Routes } from `8

'@angular/platform-browser'`3'@angular/core'`00

`'@angular/core'`2`import { NgModule } from `10`import { NgModule } from `94`import { RouterModule, Routes } from `8

'@angular/core'`2`import { RouterModule, Routes } from `0; 6 '@angular/router'1`import { RouterModule, Routes } from `6'@angular/core'`10`import { RouterModule, Routes } from `8

'@angular/platform-browser'`3'@angular/core'`13

'@angular/platform-browser'`3'@angular/core'`15

'@angular/core'`2`import { NgModule } from `10; `6`import { RouterModule, Routes } from `8

'@angular/core'`2`import { RouterModule, Routes } from `0`import { NgModule } from `41 `import { RouterModule, Routes } from `2`import { RouterModule, Routes } from `6`import { NgModule } from `48 '@angular/router'1`import { RouterModule, Routes } from `6'@angular/core'`28`import { RouterModule, Routes } from `8

'@angular/platform-browser'`3'@angular/core'`31

`'@angular/core'`2`import { NgModule } from `10`import { NgModule } from `41`import { RouterModule, Routes } from `8

; `7`import { NgModule } from `10'@angular/router'`0`import { RouterModule, Routes } from `8

import { NgModule } from `10'@angular/router'`0`import { RouterModule, Routes } from `8

import { RouterModule, Routes } from `0'@angular/core'`44`import { RouterModule, Routes } from `8

import { RouterModule, Routes } from `0'@angular/router'0 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `70`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0'@angular/router'0 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `77`import { RouterModule, Routes } from `8

`'@angular/core'`2`import { NgModule } from `80

; `7`import { NgModule } from `10'@angular/router'`0`import { RouterModule, Routes } from `8

; `7`import { RouterModule, Routes } from `0'@angular/router'0 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `90`import { RouterModule, Routes } from `8

'@angular/core'`2`import { RouterModule, Routes } from `0`import { NgModule } from `94 '@angular/router'`1`import { RouterModule, Routes } from `6`import { NgModule } from `97`import { RouterModule, Routes } from `8

'@angular/platform-browser'`3'@angular/core'`80

`'@angular/core'`2`import { NgModule } from `10`import { NgModule } from `94`import { RouterModule, Routes } from `8

'@angular/core'`2`import { RouterModule, Routes } from `0; 6 '@angular/router'1`import { RouterModule, Routes } from `6'@angular/core'`10`import { RouterModule, Routes } from `8

'@angular/platform-browser'`3'@angular/core'`93

'@angular/core'`2`import { NgModule } from `10; `6`import { RouterModule, Routes } from `8

'@angular/core'`2`import { RouterModule, Routes } from `0`import { NgModule } from `41 `import { RouterModule, Routes } from `2`import { RouterModule, Routes } from `6`import { NgModule } from `48 '@angular/router'1`import { RouterModule, Routes } from `6'@angular/core'`28`import { RouterModule, Routes } from `8

'@angular/platform-browser'`3'@angular/core'`31

`'@angular/core'`2`import { NgModule } from `10`import { NgModule } from `41`import { RouterModule, Routes } from `8

; `7`import { NgModule } from `10'@angular/router'`0`import { RouterModule, Routes } from `8

import { NgModule } from `10'@angular/router'`0`import { RouterModule, Routes } from `8

Output:

Angular ui router route move to top page năm 2024

Please Login to comment...

Similar Reads

How to make your page scroll to the top when route changes?

Clicking the "About Us" button in a React application doesn't automatically scroll to the top of the page load, as react-router-dom only changes the route without resetting the scroll position; a separate functional component is needed to ensure the new page loads from the top when the route changes. Prerequisite:ReactStyled-ComponentsuseEffect Rea

3 min read

Laravel | Route::resource vs Route::controller

In Laravel, the Route actions can be controlled by any of the following two methods, either by using Route::resource method or by using Route::controller method. But both of them have their differences. Route::resource: The Route::resource method is a RESTful Controller that generates all the basic routes required for an application and can be easi

2 min read

How to navigate to a parent route from a child route?

In angular, a root component that serves as the parent component of all the components and the rest of the other components can be called a Child Component to the root component. This structure is in a form of a tree where a component that is the parent of a child lies above the child component and this there is no direct link between them but in g

4 min read

Hide scroll bar, but while still being able to scroll using CSS

To hide the scrollbar use -webkit- because it is supported by major browsers (Google Chrome, Safari or newer versions of Opera). There are many other options for the other browsers which are listed below: -webkit- (Chrome, Safari, newer versions of Opera): .element::-webkit-scrollbar { width: 0 !important } -moz- (Firefox): .element { overflow: -mo

3 min read

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 Application Step 1: Create an Angular application using the following co

4 min read

How to attach a click and double-click event to an element in jQuery ?

In this article, we will see how to attach a click and double-click event to an element in jQuery. To attach click and double-click events, click and dbclick events are used. These events are attached with the bind() method. This method is used to attach one or more event handlers for selected elements and this method specifies a function to run wh

1 min read

How to accordion scroll to top to open content in Bootstrap ?

Bootstrap Accordions are so attractive to watch in action but when the accordion context element is so large there was a problem to get the active accordion top. but here you will learn how to achieve that facility with the help of a few lines of JavaScript code. Here we will use the scrolltop property to achieve that facility in Accordion. Syntax:

4 min read

CSS | Scroll Padding top

The Scroll Padding top is an inbuilt property in Scroll Snap module. This property set the scroll padding top longhand. This property acts as a magnet on the top of the sliding element that stick to the top of the view-port and stop the scrolling(forcefully) in that place. The Scroll Padding property is optional, this property is used when the Scro

2 min read

CSS scroll-margin-top property

The scroll-margin-top property is used to set all the scroll margins to the top of an element at once. The value specified for the scroll-margin-top determines how much of the page that is primarily outside the support should remain visible. Hence, the scroll-margin-top values represent outsets that define the scroll snap area that is used for snap

2 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 React JS using useState() hook. Prerequisite: Basic

How to navigate to the top of a page in Angular?

scrollTo() function with (0,0) coordinates to navigate to the top.18 thg 9, 2020nullHow to scroll to top on every Route click in Angular5 - GeeksforGeekswww.geeksforgeeks.org › how-to-scroll-to-top-on-every-route-click-in-an...null

How to set scroll position to top in Angular?

body. scrollTop || 0; console. log('[scroll]', scrollPosition);nullAngular Scrolling Goto Top - StackBlitzstackblitz.com › edit › angular-scrolling-goto-topnull

How do I scroll the page in Angular?

Scrolling strategies The simplest way to provide it is to use the itemSize directive on the viewport (e.g. ).nullScrolling | Angular Materialmaterial.angular.io › cdk › scrollingnull

What is routing in UI?

The UI Router is a routing framework that changes the views of the application based on not just the URL, but the state of the application as well. It helps in bridging the gap between the traditional frameworks, by allowing nested views, named views, passage of data between two different views, and so on.nullTutorial on Angular JS routing with Angular UI Router - KnowledgeHutwww.knowledgehut.com › blog › web-development › routing-with-angula...null