2017-10-09 72 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圖標基本鍵」 ' –