I
I
Igor Pavlenko2018-02-18 18:23:09
Angular
Igor Pavlenko, 2018-02-18 18:23:09

Angular 5 how to make the page load in the top position when navigating?

When building a project in production, I ran into a problem when you climb the site and scroll down, let's say, down the page and switch to another one, the browser does not rewind the page up! all sorts of crutches with relabeling work, but when there is a prerender, and the server swears that he does not know anything about document or $ (jquery) ... he broke his head how to get around this ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dasha Tsiklauri, 2018-02-18
@dasha_programmist

In the main component (presumably app.component), you subscribe to router.events, catch NavigationEnd, then check isPlatformBrowser(this.platform) and if so, call window.scrollTo(0,0).
Hints:

import {Inject, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
import {Router, NavigationEnd} from '@angular/router'; 

constructor(@Inject(PLATFORM_ID) platform:any, router:Router){
router.events.filter(ev=>ev instanceof NavigationEnd).subscribe(...)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question