Angular2:获取组件内 html 元素的引用,然后滚动到该 html 元素的顶部
Angular2: get reference of html element inside component and then Scroll to top that html element
我的 TableComponent
里面有这个 table:
<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
需要在字段中添加ElementRef
,在方法中添加nativeElement
,为可能需要的人留下答案:
@ViewChild("trackerTable") trackerTable: ElementRef;
public myMethod(): void {
this.trackerTable.nativeElement.scrollTop = 0;
}
我的 TableComponent
里面有这个 table:
<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
需要在字段中添加ElementRef
,在方法中添加nativeElement
,为可能需要的人留下答案:
@ViewChild("trackerTable") trackerTable: ElementRef;
public myMethod(): void {
this.trackerTable.nativeElement.scrollTop = 0;
}