English 中文(简体)
Configuring scroll restoration for Angular Standalone Router?
原标题:

The Angular Router has the ability to configure the router to restore scrolling and the linked documentation provides an example of how to do it when loading data.

How do we just configure the standalone router to restore the router outlet scroll to the top?

As can be seen in this demo, without any configuration, if two views have content that can be scrolled to the bottom, if one does scroll to the bottom, the viewport will stay that way when navigating across components.

https://stackblitz.com/edit/angular-adcsej?file=src%2Fmain.ts

最佳回答

If I understand correctly, you want the page to always scroll up when routing? Why such behavior? After all, when rerouting, a new page/component always starts at the top.

But one way is to set in AppModule / AppRoutingModule the scrollPositionRestoration as it is in the documentation posted by you.

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { scrollPositionRestoration:  top  }),
  ],
  exports: [RouterModule],
  providers: [ScrollToTopService],
})

top - Sets the scroll position to x = 0, y = 0 on all navigation.

Or you can create your own logic for scrolling for example via service and inject it to the module.

import { Injectable } from  @angular/core ;
import { Router, NavigationEnd } from  @angular/router ;
import { filter } from  rxjs/operators ;

@Injectable()
export class ScrollToTopService {
  constructor(router: Router) {
    router.events.pipe(
      filter(event => event instanceof NavigationEnd)
    ).subscribe(() => {
      window.scrollTo(0, 0);
    });
  }
}

So instead of scrollPositionRestoration you would add inside module

providers: [ScrollToTopService]
问题回答

When using the standalone Angular bootstrapApplication function to provide the router with the provideRouter function, the router can be configured like this to always scroll back to the top.

const scrollConfig: InMemoryScrollingOptions = {
  scrollPositionRestoration:  top ,
  anchorScrolling:  enabled ,
};

const inMemoryScrollingFeature: InMemoryScrollingFeature =
  withInMemoryScrolling(scrollConfig);
bootstrapApplication(App, {
  providers: [provideRouter(routes, inMemoryScrollingFeature)],
});

Here s a Demo: https://stackblitz.com/edit/angular-quflkz?file=src%2Fmain.ts

The relevant documentation.

This demo shows what happens if scroll restoration is not used.

https://stackblitz.com/edit/angular-adcsej?file=src%2Fmain.ts





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签