2017-10-09 71 views
0

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

这些都是我在得到数据代码: getProducts() { this.productService .getProducts(this.args) .subscribe( data => { this.data = data; this.loadingService.hide(); this.document.body.scrollTop = 0; }, (error) => console.log(error); }
); }

+2

window.scrollTo(0,0)适用于ngOnInit()。 –

+0

我也尝试过使用它,但它不工作@ harold_mean2 –

+0

尝试'setTimeout((=){window.scrollTo(0,0);},0);' –

回答

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

它不适用于我的目的。我不知道为什么。 **这是我的代码为我paginator.component.html ** '<按钮类= “分页程序导航-先前的UI图标基本键” [class.disabled] = “的PageIndex === 0” (点击)= “onPrevious()”> <按钮(点击)= “GOTOP()” 类= “分页程序导航-下一UI图标基本键” ' –