在angular2中滚动顶部 [英] Scroll Top in angular2
本文介绍了在angular2中滚动顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用angular2 Web应用程序,需要以下方面的帮助.
我的页面包含多个组件.我想在用户单击按钮时滚动页面的顶部.我试过了
document.body.scrollTop = 0;
,但这在Chrome中不起作用.我尝试了document.documentElement.scrollTop = 0; window.scrollTo(0,0);但不起作用
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
推荐答案
像这样导入,
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;
这篇关于在angular2中滚动顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文