在 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屋!
查看全文