Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll Top in angular2

I am working on angular2 web application where I need help on the following. My page consists of multiple components. I want to scroll top of the page when user clicks a button. I tried document.body.scrollTop = 0; but this is not working in Chrome. I Tried document.documentElement.scrollTop=0;window.scrollTo(0, 0); but not working

like image 731
Vinodh Ram Avatar asked Apr 29 '17 05:04

Vinodh Ram


1 Answers

import like this,

import { Inject} from "@angular/core";
import { DOCUMENT } from '@angular/platform-browser';

In your constructor add this,

constructor(@Inject(DOCUMENT) private document: Document) { }

Then you can set the scroll anywhere like this,

this.document.body.scrollTop = 0;
like image 101
jaseelmp Avatar answered Oct 11 '22 00:10

jaseelmp