1

I have the list page. when i scroll down and click on one of the entry, it redirects to the detail page which opens up from the top. Now when i go back to the list page it should open up at the same scroll position as it was when i click on one of the entry. i am using lazy loading and my angular version is 12.

also i had to use this follwoing code to open the detail page at the top..

app.component.html

export class AppComponent implements OnInit {
title = 'app';
UserLanguage: string;
showLoader: boolean;
isDevEnv: boolean = false;
constructor(
  private translate: TranslateService,
  private router: Router,
  private storageAdapter: StorageAdapter,
  private commonService: CommonFunctionsService,
  private viewportScroller: ViewportScroller,
) {
  this.location.subscribe((ev: PopStateEvent) => console.log('ev: PopStateEvent', ev))
  this.router.events.subscribe((event: Event) => {
    if (event instanceof NavigationEnd) {
      (<any>window).ga('set', 'page', event.urlAfterRedirects);
      if (this.storageAdapter.getUserId()) {
        (<any>window).ga('set', 'userId', this.storageAdapter.getUserId());
        (<any>window).ga('set', 'dimension1', this.storageAdapter.getUserId()); GLUserId
      }
      (<any>window).ga('send', 'pageview');
    }
    if (event instanceof Scroll) {
      console.log('event', event)
      if (event.anchor) {
      // anchor navigation
      /* setTimeout is the core line to solve the solution */
        setTimeout(() => {
          this.viewportScroller.scrollToAnchor(event.anchor);
        })
      } else if (event.position) {
        // backward navigation
        this.viewportScroller.scrollToPosition(event.position);
      } else {
        // forward navigation
        this.viewportScroller.scrollToPosition([0, 0]);
      }
    }
  });
}

}

i have tried this in app-routing.component.html but it doesnt work..

@NgModule({
imports: [RouterModule.forRoot(routes, { relativeLinkResolution: 'legacy', 
scrollPositionRestoration: 'enabled' })],
exports: [RouterModule],
})

Any help or guidance would be appreciated, thanks in advance.

purvaDevIt
  • 11
  • 1

0 Answers0