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
9 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;
7 Comments
I solved my angular scrolling problem using data binding:
<div class="container" [scrollTop]="scrollTop"> ... </div>
with the styles:
.container {
  height: 100%;
  scroll: auto;
  position: relative;
}
1 Comment
scrollTop doesn't change in the component, you won't see anything happen. For instance if you set the default value to 0 in the component, scroll the page, and then set it to zero after a button click or something.I propose to write directive for that. Make sure to import it in the module you are using.
import { Directive, HostListener } from '@angular/core';
@Directive({
  selector: '[scrollToTop]'
})
export class ScrollToTopDirective {
  @HostListener('click')
  public onClick() {
    if (window && window.pageYOffset) {
      window.scroll(0, 0);
    }
  }
}
and use it like below
<button scrollToTop>Scroll to Top</button>
Consider also to add the prefix to the directive in compliance with Angular best practices.
2 Comments
Please use below code. For my case
this.document.body.scrollTop = 0
not working but
this.document.documentElement.scrollTop = 0
working. So may be some browser dependency.
import { Inject} from "@angular/core";
import { DOCUMENT } from '@angular/platform-browser';
constructor(@Inject(DOCUMENT) private document: Document) { }
this.document.documentElement.scrollTop = 0;
Comments
html code :
<div class="scroll-to-top" [ngClass]="{'show-scroll': navIsFixed}">
            <button (click)="scrollToTop()">
                Top
            </button>
        </div>
css code:
.scroll-to-top {
    position: fixed;
    bottom: 15px;
    right: 15px;
    opacity: 0;
    transition: all .2s ease-in-out;
}
.show-scroll {
    opacity: 1;
    transition: all .2s ease-in-out;
}
ts code:
import {Component, OnInit, Inject, HostListener} from '@angular/core';
import { DOCUMENT } from "@angular/platform-browser";
import { BrowserModule } from '@angular/platform-browser';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Hard Hitter@Cool';
  navIsFixed: boolean;
  constructor(@Inject(DOCUMENT) private document: Document){
  }
  @HostListener("window:scroll", [])
  onWindowScroll() {
      if (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop > 100) {
          this.navIsFixed = true;
      } else if (this.navIsFixed && window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop < 10) { this.navIsFixed = false; } } scrollToTop() { (function smoothscroll() { var currentScroll = document.documentElement.scrollTop || document.body.scrollTop; if (currentScroll > 0) {
              window.requestAnimationFrame(smoothscroll);
              window.scrollTo(0, currentScroll - (currentScroll / 5));
          }
      })();
  }
}
Comments
If it is not window scroll, but just div with his own scroll, this should work:
Gloabal service WindowRef:
import { Injectable } from '@angular/core';
function _window(): any {
  // return the global native browser window object
  return window;
}
@Injectable()
export class WindowRef {
  get nativeWindow(): any {
    return _window().document;
  }
}
Add it to constructor:
constructor(private winRef: WindowRef) {}
And in the code, where you want to put it just add this line:
this.winRef.nativeWindow.getElementsByClass('nameOfClass')[0].scroll(0, 0);
Of course you can also use other selectors like: getElementByTagName, getElementById, getElementByName ...









document.body.scrollTop = 0;should work, anyways whats the error ?