Angular 2在加载后滚动到顶部

时间:2017-10-09 02:17:04

标签: angular typescript loading scrolltop

我在获取数据时添加了一个加载。在我的视图列表中有分页和页面大小。所以我的问题是每当我尝试更改页面大小并在加载后立即移动到其他页面时,它就不会滚动到顶部。我在加载后添加了this.document.body.scrollTop = 0;,但它无法正常工作。

这些是我获取数据的代码:

`getProducts() {
    this.productService
       .getProducts(this.args)
       .subscribe(
          data => {
              this.data = data;
              this.loadingService.hide();
              this.document.body.scrollTop = 0;
          },
          (error) =>
                console.log(error);
          }       
      );
    }`

3 个答案:

答案 0 :(得分:0)

试试这样:

<强> component.html

constructor(props) {
    super(props);
    this.state = {
        user: {},
        loading: true
    }
}

<强> component.ts

<button class="btn btn-info" (click)="goTop()">goTop</button>

答案 1 :(得分:0)

 import { Component, ViewChild } from '@angular/core';
    import { Content } from 'ionic-angular';

    @Component({...})
    export class MyPage{
      @ViewChild(Content) content: Content;

添加到内部订阅

this.content.scrollToTop();

全球化志愿服务青年; http://ionicframework.com/docs/api/components/content/Content/

答案 2 :(得分:0)

ngOnit 中使用window.scrollTo(0,0);。所以它应该看起来像这样

ngOnInit() { window.scrollTo(0,0); }