-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changing route doesn't scroll to top in the new page #7791
Comments
dup of #6595 ? |
I don't think so. |
#6946 shows a workaround |
It isn't expected behaviour for me. Expected behaviour for me is: When navigating back to another route the page should be scrolled to position at which it was before going forward. This is the behaviour currently implemented in all browsers when navigating between normal pages (not SPA). But I never seen this behaviour in any implementation of angular1 router. It is very annoying when you scroll down some list, click on list item to see details page, scrolling down details, then click browser
The github, for example, implements this correctly: Open issues list and scroll down, then open any issue details, then click It will be very cool if angular2 router able to do it. It is really a killer feature I never seen in any other routers. Edit |
may be would be better ta have some attribute or param to change this default scroll behaviour? |
Is there a recommended method for scrolling to the top in RC.4? The suggested workaround in #6946 no longer works. |
In my case the
|
Any solution? None of the proposed solutions work. ngAfterViewChecked() { |
@Ariix wouldn't that scroll after every time change detection runs? |
@cortopy Yes, yes it does. So still looking |
Any update or milestone for this? |
Any update on this? Its annoying when you scroll down some list, click a list item to see details (which is on another route) you have to scroll up. If the next details page's length is small and the list page is very long and by chance you clicked the last item in the list. It would give an impression that there aren't any contents on details page (as the user has to scroll up to view contents). |
@zoechi I believe that comment is about URL with hash, in my case I don't' have any hash: List page has a url: details page has a url: |
@naveedahmed1 you can still call |
@zoechi - Can you share the full code? i have parent child scenario where child routes are changing |
My work around involves the ElementRef class. constructor(
private route: ActivatedRoute,
private service: GuideService,
private router: Router,
private element: ElementRef
) {
this.scrollUp = this.router.events.subscribe((path) => {
element.nativeElement.scrollIntoView();
});
} |
How about this?
|
This seems to remember the scroll position (in Chrome and Firefox but not IE), but I have no idea why. |
@SmaugTheGreat Yes, that does work! But how is that even possible that it remembers the last scroll position? |
Its strange, when using @SmaugTheGreat solution i.e. window.scroll(0, 0); it remembers scroll position. |
why doesn't NavigationEnd contain params, query params etc of the route? only string? |
A mixture of Smaugs solution above and Guilherme Meireles on stackoverflow does the trick for me. Also unsubscribing in ngOnDestroy to avoid leakage.
|
use 'ng2-page-scroll' module. As a quick response, i am posting one of my component. This one worked for me. import {Component, OnInit, Inject } from '@angular/core'; @component({ routerSubscription: Subscription; constructor(private router: Router, private SharedService: AppSharedService, private pageScrollService: PageScrollService, @Inject(DOCUMENT) private document: any,) { } } |
@shanehickeylk If you just called router.events..... .subscribe() directly, there is no subscription to unsubscribe. Update: yes you do :) sorry guys |
@spock123 Here is a good article by Brian Love, where he goes into some additional detail on the topic: |
@mkhan004 interesting but may I ask, therefore are you using a special lib/polyfill which override |
@peterpeterparker It is native browser behavior. Word of advice - don't use W3Schools, it is known for imprecise or just plain wrong information. It's better to use MDN; a relevant page here: https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo |
@mgol thx 👍 |
Worth mentioning: Current versions of Chrome and Firefox seem to work though. |
@MartinMa That's a good point; the relevant Can I Use page is https://caniuse.com/#feat=css-scroll-behavior Note that even just omitting if ('scrollBehavior' in document.documentElement.style) {
window.scrollTo({
behavior: 'smooth',
left: x,
top: y,
});
} else {
window.scrollTo(x, y);
} |
so what's the best option to fix this issue? |
For documentation, see `RouterModule.scrollPositionRestoration` Fixes angular#13636 angular#10929 angular#7791 angular#6595
Closed by #20030 |
Closing based on being fixed as above. |
use |
just have a look at Angular 6.1. It was fixed, and probably in a future release, it will be default |
here is the best solution!!! import { Component } from '@angular/core'; @component({ constructor(private router: Router) {
} |
A workaround is to put this in application.run() - at least in an old version of Angular. Might help someone!
|
@JamieFarrelly an official solution has been released in angular 6.1
@see documentation:
[edit] so in your case: @NgModule({
imports: [RouterModule.forRoot(routes, {
scrollPositionRestoration: 'top'
})],
exports: [RouterModule]
}) |
This won't work with Angular Material |
@odahcam I'm using MatDrawerContainer and it works form with ViewChild
|
Yeah, I'm able to scroll the container, but how to make the router automatically scroll it for me? (like it would with viewport) |
or I misunderstood you? |
Actually I'm using something like that, but I think the router should have some scrollable container configuration, beucase it tries to scroll by the viewport when you enable the Your code does the job as mine does too, but both of us are unable to take profit from the native router functionality that does the scrolling job for us. To do that, we would need to tell the router to use our custom scrollable container instead of the viewport. I noticed that the Router makes use of a guy called class AppModule {
constructor(router: Router, viewportScroller: ViewportScroller, store: Store<AppState>) {
router.events.pipe(filter(e => e instanceof Scroll), switchMap(e => {
return store.pipe(first(), timeout(200), map(() => e));
}).subscribe(e => {
if (e.position) {
viewportScroller.scrollToPosition(e.position);
} else if (e.anchor) {
viewportScroller.scrollToAnchor(e.anchor);
} else {
viewportScroller.scrollToPosition([0, 0]);
}
});
}
} That makes me think that the only way to integrate something like a fullscreen Unfortunately the docs for this matter is a little bit inexistent, so things are even more difficult. |
Angular 6.1 and later: RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled'}) |
@odahcam I've linked your comments to a related Angular Material issue: angular/components#4280. It would be great if you could provide a Stackblitz example and some additional information there. |
@Splaktar all right, posted. |
For those who cannot use filter(), map() etc operators on Observable add this:
|
To others looking for a simple and clean solution for keeping the scroll position on back navigation and scroll to top on forward navigations, this is the solution. Will be default Angular behaviour in the future (described by the angular specifications: https://angular.io/api/router/ExtraOptions) |
I would like to add a half thumb up! 😅 If you have the following // ...
const routerOptions: ExtraOptions = {
useHash: false,
anchorScrolling: 'enabled',
scrollPositionRestoration: 'enabled'
};
// ... in order to enable anchor links, it'll always go on top, even if the anchor is at the bottom of the page. But if you're not using anchor navigation, it's an awesome fix! 😉 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
When navigating from one route to another then scrolling down a bit and navigate back to the previous route, the page remains at the same scroll position.
Current behavior
Navigate from one view to another then scroll down a bit and navigate back to the previous view. the page remains at the same scroll position.
Expected/desired behavior
When navigating back to another route the page should be scrolled to the top.
The text was updated successfully, but these errors were encountered: