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
scrollPositionRestoration has several problems #24547
Comments
Thanks for the detailed issue report! Great to get feedback on this new feature and hopefully get these things addressed quickly. |
Closed by #20030 |
@mhevery @jasonaden please reopen. This issue is not about a missing scroll position restoration. It's precisely about the new scroll position restoration feature that was introduced in 6.1.0-beta.1. And it thus can't be closed by #20030: this issue is precisely about several problems in the code and documentation introduced by #20030. |
Sorry |
I'd like to add in combination with Ionic 4 the ViewportScroller's Scroll event position property is always [0, 0] |
Just chiming in here, my initial expectation of this feature was the following:
To test this idea I tried the following as a simple use-case: With This simple use-case doesn't seem to be resolved by the aforementioned #20030. Is that PR supposed to solve this use case? But, just like @jnizet after upgrading a simple project to I'm curious about the following:
|
Just leaving this here for all future visitors, as I did some debugging to determine why this wasn't working. For anyone who sees this in the future. If you have the following code in your stylesheet...
This expected functionality will appear to be a bug for you. Removing that style has a decent potential to fix this issue and make this PR do what was intended. Granted, removing that style may make a bunch of other things break, but that's a different problem. Ninja edit: Working Version (My own personal anecdote): Non-working version (as reported by @crisbeto ): |
@damienwebdev Thanks for that note on the body height. That very well could help quite a number of people On the other things discussed in this issue, I've talked with @vsavkin about it and we're going to solve this in a couple ways. First is some documentation to make sure your use cases are discussed and solutions given for some of the common patterns. That documentation will be forthcoming. Another is to fix an issue that was introduced some time ago where it's possible to have a |
@jasonaden Could you tell, if following use case will be covered: We are using perceived performance and intentionally are loading the data every time on component initialization, instead of caching, thus the app could be used with multiple devices at the same time. Thank you in forward. |
What's the progress on this issue? |
This is a must after watching Jason's video about ng6.1 features where he says : [...] be aware on when and where you're rendering out your position. |
@mhevery (Edit: Sorry nevermind!) I know one could preload all the data with Resolve from '@angular/router'. This would be fine and we could provide for each component an equal named class which could resolve all the needed data. Another also very common practice is to use perceived performance. This is the way we decided to interact with preloading data. In our application we have a lot of times a list, which would show some grey boxes unless the data arrives and the list is shown. As the component already is constructed and all the events are fired this would mean we would need to have the possibility to provide some asynchronous way to tell the data has been loaded for the scroll restoration service. Do you see the possibility to provide a: 1. CustomScrollPositionRestorationStrategy or interface AfterContentReady {
ngAfterContentReady(): Promise<boolean>;
} I am also open for other ways. Maybe one would have some suggestions? EDIT: Excerpt from: export class PendingRacesComponent {
scrollPosition: [number, number];
races: Array<RaceModel>;
constructor(route: ActivatedRoute, private router: Router, private viewportScroller: ViewportScroller) {
this.races = route.snapshot.data['races'];
this.router.events.pipe(
filter(e => e instanceof Scroll)
).subscribe(e => {
if ((e as Scroll).position) {
this.scrollPosition = (e as Scroll).position;
} else {
this.scrollPosition = [0, 0];
}
});
}
ngAfterViewInit() {
this.viewportScroller.scrollToPosition(this.scrollPosition);
}
} |
Whats the status on the issue? Hash anyone found a perfect solution/ when will this issue be fixed? |
One additional note I would like to mention here: (I did workaround this by setting all parent elements until the body to position: static / relative (they were position: absolute before), which introduced other problems, e.g. with modal dialogs, backdrops or scroll bar issues with fixed elements on different OS with different settings..)Feature request:
Potential problems
|
This feature breaks server side rendering with asp .net . |
It's not quite clear, is there any way to set the scroll policy for certain routes? |
On Chrome, Using min-height doesn't break auto-scrolling and will force body t be at least screen height.
|
Thanks for the feedback so far on this issue. We're going to be making some minor improvements to the current implementation, but also will be adding some new documentation in the next few weeks to help with some of the scenarios you guys have been having trouble with. Thanks for the patience on this one. |
new insights... but when i use something like this, it doesn't: could somebody explain why the above code is not producing the same behaviour as Location.back(), and what are the options? |
@lnaie That is intended. This feature will work when use browser's navigating back. Redirecting to |
I would be very happy to review and help merge updates to the docs that highlight these known issues, if you would like to send in some PRs. |
Oh and just to clarify a few things... The P labels are not the same as the priority that I mentioned above. Once a piece of work is deemed complex enough it has to go into our team prioritization process amid many other tasks, of which quite a few are not even visible publicly. And also, open issues are never closed by any bot. Only closed issues that have not received activity for a long time are locked. This is a common misconception but I am not sure how we can communicate that better. |
@petebacondarwin Thanks for the reply. This response will swerve into off topic for this thread, but, of course I'd be happy to submit PRs.. My most pressing issue would be: #30477. Happy to discuss elsewhere. |
@michaelurban the workaround I posted above (or a variation on that) has been working for me in a production app for over a year. The issue isn't that the documentation is wrong, but rather that the To me, this feels like a "recipe" for lack of a better term. Or it could be solved with something similar to the Common Routing Tasks section in the router guide. Because implementing your own |
@DeanPDX Yes, as I previously stated, in this case the documentation is incomplete/oversimplified rather than incorrect. The feature also needs some work aside from the documentation but, the core issue is: Sometimes in Angular things are not as simple or as solid the documentation would have you believe. If a feature has several caveats, special cases, or, has not worked for years (as in #30477), that should be spelled out clearly in the docs. |
we should remove
to prevent scroll trigger too fast, before ngOnInit. I dont know why my component both reconstruct and re run ngOnInit on back. I just back only. and it cannot run fast enough
|
Hi everybody, concerning this issue, you could have a look at NgxScrollPositionRestoration. A library for scroll position restoration in Angular. The library supports scroll position restoration on:
View the demoCompatibility
|
Hello everyone, I have the similar issue. I have used mat-paginator for pagination and on the new/next page click the scrollbar should reset to top for the page change event. Instead it is shortening, could any one please help me with this. @TheJLifeX @hiepxanh |
This makes a small update to the example so that it shows a use-case that's not already covered by the 'enabled' option. The existing example would get identical behavior to `scrollPositionRestoration: 'enabled'` with `anchorScrolling: 'enabled'`. The updated example shows a use-case for when custom scrolling _would_ be needed. For example, when data is fetched because it is not available immediately or through a resolver. This is one of the cases described in angular#24547 This update is sufficient to address all of the documentation problems noted in the aforementioned issue. Another fix should be made to address the problem that scroll restoration needs to be delayed until after CD has run so the update block of the activated component's template is run.
This makes a small update to the example so that it shows a use-case that's not already covered by the 'enabled' option. The existing example would get identical behavior to `scrollPositionRestoration: 'enabled'` with `anchorScrolling: 'enabled'`. The updated example shows a use-case for when custom scrolling _would_ be needed. For example, when data is fetched because it is not available immediately or through a resolver. This is one of the cases described in #24547 This update is sufficient to address all of the documentation problems noted in the aforementioned issue. Another fix should be made to address the problem that scroll restoration needs to be delayed until after CD has run so the update block of the activated component's template is run. PR Close #46201
This makes a small update to the example so that it shows a use-case that's not already covered by the 'enabled' option. The existing example would get identical behavior to `scrollPositionRestoration: 'enabled'` with `anchorScrolling: 'enabled'`. The updated example shows a use-case for when custom scrolling _would_ be needed. For example, when data is fetched because it is not available immediately or through a resolver. This is one of the cases described in #24547 This update is sufficient to address all of the documentation problems noted in the aforementioned issue. Another fix should be made to address the problem that scroll restoration needs to be delayed until after CD has run so the update block of the activated component's template is run. PR Close #46201
This makes a small update to the example so that it shows a use-case that's not already covered by the 'enabled' option. The existing example would get identical behavior to `scrollPositionRestoration: 'enabled'` with `anchorScrolling: 'enabled'`. The updated example shows a use-case for when custom scrolling _would_ be needed. For example, when data is fetched because it is not available immediately or through a resolver. This is one of the cases described in #24547 This update is sufficient to address all of the documentation problems noted in the aforementioned issue. Another fix should be made to address the problem that scroll restoration needs to be delayed until after CD has run so the update block of the activated component's template is run. PR Close #46201
Hi @HARSHITHASAI-A, import { Location } from '@angular/common';
class SomeComponent {
constructor(private location: Location) { }
goBack() {
this.location.back(); // See: https://angular.io/api/common/Location#back
}
goForward() {
this.location.forward(); // See: https://angular.io/api/common/Location#forward
}
} If you are using NgxScrollPositionRestoration, you can import it like this (with NgxScrollPositionRestorationModule.forRoot({
debug: true
}) I hope this can help you. |
…e rendered Currently, the scroll event is fired immediately after the `NavigationEnd`. However, this is problematic because a change detection has not been able to run, meaning that Angular will not yet have run the update block of the component templates being rendered as part of the navigation. This change delays the scroll event using a `setTimeout`, which will allow Angular's change detection to run before the scroll restoration is performed. fixes angular#24547
…e rendered Currently, the scroll event is fired immediately after the `NavigationEnd`. However, this is problematic because a change detection has not been able to run, meaning that Angular will not yet have run the update block of the component templates being rendered as part of the navigation. This change delays the scroll event using a `setTimeout`, which will allow Angular's change detection to run before the scroll restoration is performed. fixes angular#24547
…e rendered Currently, the scroll event is fired immediately after the `NavigationEnd`. However, this is problematic because a change detection has not been able to run, meaning that Angular will not yet have run the update block of the component templates being rendered as part of the navigation. This change delays the scroll event using a `setTimeout`, which will allow Angular's change detection to run before the scroll restoration is performed. fixes angular#24547
…e rendered Currently, the scroll event is fired immediately after the `NavigationEnd`. However, this is problematic because a change detection has not been able to run, meaning that Angular will not yet have run the update block of the component templates being rendered as part of the navigation. This change delays the scroll event using a `setTimeout`, which will allow Angular's change detection to run before the scroll restoration is performed. fixes angular#24547
…e rendered (#47563) Currently, the scroll event is fired immediately after the `NavigationEnd`. However, this is problematic because a change detection has not been able to run, meaning that Angular will not yet have run the update block of the component templates being rendered as part of the navigation. This change delays the scroll event using a `setTimeout`, which will allow Angular's change detection to run before the scroll restoration is performed. fixes #24547 PR Close #47563
It seems that af8afee addresses some change detection portion of this issue, though I am not if that anyhow resolves the issue for those people that don't have html/body their scrollable element. It would be nice if we could pass either a specific selector for scrollable container ourselves or some kind of custom strategy for resolving the component dynamically ourselves. |
…e rendered (angular#47563) Currently, the scroll event is fired immediately after the `NavigationEnd`. However, this is problematic because a change detection has not been able to run, meaning that Angular will not yet have run the update block of the component templates being rendered as part of the navigation. This change delays the scroll event using a `setTimeout`, which will allow Angular's change detection to run before the scroll restoration is performed. fixes angular#24547 PR Close angular#47563
I have a simple Angular 14 app where everything is served prerendered on S3. Angular's scrollPositionRestoration: 'enabled' works for my web app as long as the screen is large. But on a mobile phone screen scrollPositionRestoraiton stops working. Anyone know why this would happen? I ran into problems trying DeanPDX's solution, which has an Angular 9 demo. For example, I wasn't able to import eeinject in the app.module.ts. Was anyone able to get this solution working in Angular 14? If so, what changes did you have to make? NgxScrollPositionRestoration is only compatible with Angular 13 and earlier, or did someone get this to work in Angular 14? Small screen size issue happened in Chrome browser on a large monitor with browser shrunk down, on a chromebook again with the browser shrunk down to mobile phone size, and an iphone using Chrome browser and Safari. I also tried anchorScrolling as a solution but had the same result: works on wide screen but not on cell phone sized screen. |
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. |
…e rendered (angular#47563) Currently, the scroll event is fired immediately after the `NavigationEnd`. However, this is problematic because a change detection has not been able to run, meaning that Angular will not yet have run the update block of the component templates being rendered as part of the navigation. This change delays the scroll event using a `setTimeout`, which will allow Angular's change detection to run before the scroll restoration is performed. fixes angular#24547 PR Close angular#47563
I'm submitting a...
Current behavior
I started experimenting with the new
scrollPositionRestoration
feature in the RouterModule extra options. I expected scroll restoration to work by default when setting the property to 'enabled', but it doesn't. And the documentation has issues, too.Expected behavior
The documentation says:
So I naïvely thought that setting the flag to 'enabled' would be sufficient to restore the scroll position. But it isn't.
Indeed, the scroll event is fired, and the scroll position is restored, before the
ngAfterViewInit
hook of the activated component has been called. So the view of the component is not ready yet when the router tries to restore the scroll position (i.e. there is no way to scroll to the end of a long list, because the list isn't there yet).And even if it was restored after the view is ready, that would only work if the activated component used a resolved guard to load the data.
So, the documentation should, IMHO, at least indicate that restoring the scroll position always requires to
I read the remaining of the documentation, which has examples about doing this kind of stuff (although it doesn't really say that they're required). But those examples are all incorrect.
Here's the first example:
This example uses a Store service, which is not part of Angular (I guess it's part of ngrx). So that makes it hard to understand and adapt for those who don't use ngrx.
Besides, it doesn't compile, because a closing parenthesis is missing, and because
e
is of type Event, and not of type Scroll, and thus has noposition
property.The second example is the following:
It doesn't compile because it still uses an old, non-pipeable operator, and because, once again,
e
is of type Event, not Scroll.But even after fixing the compilation errors, it doesn't work because the view hasn't been updated with the new list yet when
viewportScroller.scrollToPosition(e.position);
is called.So the code would have to be changed to the following in order to compile and work as expected
I think that none of these solutions is really simple enough, though. Here are two ideas that could maybe make things easier:
Minimal reproduction of the problem with instructions
Here's a repo illustrating the various issues and solutions presented above: https://github.com/jnizet/scrollbug. It's a standard angular-cli project. I can't run it in stackblitz unfortunately (probably because Stackblitz doesn't support the beta release of Angular).
What is the motivation / use case for changing the behavior?
First, the documentation should be fixed and made clearer
Second, it should be way easier to make that feature work. See ideas above.
Environment
The text was updated successfully, but these errors were encountered: