Angular 2在加载后滚动到顶部

问题描述 投票:0回答:3

我在获取数据时添加了一个加载。在我的视图列表中有分页和页面大小。所以我的问题是每当我尝试更改页面大小并在加载后立即移动到其他页面时,它不会移动到顶部。我在加载后已经添加了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);
          }       
      );
    }`
angular typescript loading scrolltop
3个回答
0
投票

试试这样:

component.html

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

component.ts

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

export class Component {
    constructor(
        @Inject(DOCUMENT) private document: Document
    ) {}
    goTop() {
        this.document.body.scrollTop = 0;
    }
}

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

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

添加到内部订阅

this.content.scrollToTop();

参考; qazxsw poi


0
投票

在ngOnit中使用http://ionicframework.com/docs/api/components/content/Content/。所以看起来应该是这样的

window.scrollTo(0,0);

© www.soinside.com 2019 - 2024. All rights reserved.