I suggest writing a directive for this. Be sure to import it into the module you are using.
import {Directive, ElementRef, HostListener} of '@ angular / core';
@Directive({
selector: '[scrollToTop]'
})
export class ScrollToTopDirective {
constructor(private elementRef: ElementRef) {
}
@HostListener('click')
public onClick() {
if (window && window.pageYOffset) {
window.scroll(0, 0);
}
}
}
and use it as below
<button scrollToTop>Scroll to Top</button>
source
share