2016-09-16 10 views
1

我有這個表我TableComponent內:Angular2:進去組件的HTML元素的引用,然後滾動到頂部是html元素

<table #trackerTable> 

我想滾動到它的上面,當有事情發生,所以我試着中引用,在我的TableComponent

@ViewChild("trackerTable") trackerTable; 

myMethod()被觸發我嘗試scrollTo(0,0)

public myMethod() { 
    this.trackerTable.scrollTo(0,0); // TypeError: this.trackerTable.scrollTo is not a function 
} 

,但我得到的錯誤:TypeError: this.trackerTable.scrollTo is not a function

回答

3

需要添加ElementRef到現場和方法nativeElement,留下這些問題的答案誰可能需要它:

@ViewChild("trackerTable") trackerTable: ElementRef; 


public myMethod(): void { 
    this.trackerTable.nativeElement.scrollTop = 0; 

}