【问题标题】:From one component trigger scroll to top of element in another component从一个组件触发滚动到另一个组件中的元素顶部
【发布时间】:2017-01-23 17:06:54
【问题描述】:

我有这张表,它是TableComponent 的一部分:

<table class="th-tracker-table th-environmental-table" #trackerTable>

swapProjectsGroups() 方法被触发时,我想从MyComponent 访问它并滚动到组件顶部

export class MyComponent {

    @ViewChild("#trackerTable") trackerTable: ElementRef;

    public swapProjectsGroups() {
        this.trackerTable.nativeElement.scrollTo(0,0); //Error: Cannot read property 'scrollTo' of undefined
    }
}

但我得到了Cannot read property 'scrollTo' of undefined,我是否缺少初始化 nativeElement 的内容?

angular2 的实现方式是什么?

【问题讨论】:

  • 你知道怎么做吗?

标签: angular typescript angular2-template angular2-directives


【解决方案1】:

试试这个: @ViewChild("trackerTable") trackerTable: TableComponent;

【讨论】:

    【解决方案2】:

    我猜你不能用模板变量来做。尝试将您的表格包装成一个组件,然后

    @ViewChild(YourComponent) _yourComponent: YourComponent;
    

    记住 _yourComponent 首先在 ngAfterViewInit 事件中可用。

    【讨论】:

    • 看来我需要添加nativeElement:this.trackerTable.nativeElement.scrollTo(0,0); b 但现在我无法读取未定义的属性“nativeElement”
    • this.trackerTable.nativeElement.scrollTop = 0;并记住您触发呼叫的生命周期事件。
    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 2017-11-03
    • 2017-03-11
    • 2021-02-02
    • 2018-05-14
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    相关资源
    最近更新 更多