I managed to solve the problem, now only needing adjustments to leave 100%.
In the catalog screen ngOnInit, I added the following:
window.addEventListener("scroll", this.scroll, true);
This adds the signature to the scroll event by executing the scroll (this.scroll) function for each change in the scroll value.
The function was thus:
scroll = (event: any): void => {
this.scrollNumber = event.srcElement.scrollTop;
// console.log(event);
// console.log("posição " + this.scrollNumber);
};
Since my behavior of accessing the product detail screen was triggered by the method inside the card, I passed the value of the saved scrollNumber in the function to the card per parameter.
<app-card-produto *ngFor="let p of e.produtos" [produto]="p"
[scrollPosition]="scrollNumber">
</app-card-produto>
In the method that redirects to the detail screen passing all irrelevant parameters here, I save the scroll value in Sessionstorage as follows:
let scroll = this.scrollPosition;
sessionStorage.setItem("scrollPosition", JSON.stringify(scroll));
Once this is done, when I return to the catalog screen, I create a method to read this variable in the session and set its value to the page scroll. In ngOnInit I call the method this.recoverScroll();. And in the method I have the following:
recoverScroll() {
let scrollSaved = JSON.parse(sessionStorage.getItem("scrollPosition"));
setTimeout(() => {
document.getElementsByClassName(
"ng-scroll-viewport"
)[0].scrollTop = scrollSaved;
}, 10);
//reset scroll
sessionStorage.setItem("scrollPosition", JSON.stringify(0));
}
To ensure that access by no other route other than Detail -> Cataloguepage ends up rolling to a product that does not make sense in context, at the end of the zero I method the session variable.
Completion: Maybe it is not one of the best practices to set the value using 'ng-scroll-viewport', but I could not using any other way I found through the forums. Just need now to add an animation to make the scroll smooth. I’ve seen that in this case it won’t be possible through CSS, but as soon as I resolve update the answer.
I just started working with programming and even less with Angular, so surely there must be a more optimized way to do this, but I hope somehow I’ve helped!