From 1a9bd9d63ebf7155eee1a7495a1682e976d14c95 Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Thu, 23 Dec 2021 15:14:02 +0100 Subject: [PATCH] fix(news): fix scroll position when going on an article page --- .../post-details/post-details.component.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/app/post/components/post-details/post-details.component.ts b/src/app/post/components/post-details/post-details.component.ts index 317585f70..856e87766 100644 --- a/src/app/post/components/post-details/post-details.component.ts +++ b/src/app/post/components/post-details/post-details.component.ts @@ -1,6 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; -import { ActivatedRoute, Router } from '@angular/router'; +import { ActivatedRoute } from '@angular/router'; import { RouterListenerService } from '../../../services/routerListener.service'; import { Post } from '../../models/post.model'; import { PostService } from '../../services/post.service'; @@ -11,15 +11,17 @@ import { PostService } from '../../services/post.service'; styleUrls: ['./post-details.component.scss'], }) export class PostDetailsComponent implements OnInit { + public post: Post; + constructor( private activatedRoute: ActivatedRoute, - private router: Router, private postService: PostService, private sanitizer: DomSanitizer, private routerListener: RouterListenerService ) {} - post: Post; + ngOnInit(): void { + this.resetScroll(); if (history.state.data) { this.post = new Post(history.state.data); this.post.safeHtml = this.sanitizer.bypassSecurityTrustHtml(this.post.html); @@ -35,4 +37,10 @@ export class PostDetailsComponent implements OnInit { public backToPosts(): void { this.routerListener.goToPreviousUrl(); } + + private resetScroll(): void { + if (window.scrollY) { + window.scroll(0, 0); // reset the scroll position to the top left of the document. + } + } } -- GitLab