【问题标题】:ag-grid angular get element refag-grid 角度获取元素参考
【发布时间】:2019-09-11 20:57:22
【问题描述】:

如何在 Angular 中获取网格元素的 DOM 引用?似乎没有一个简单的方法。我试过了:

标记:

<ag-grid-angular #logGrid></ag-grid-angular>

ts:

@ViewChild('logGrid') logGrid: AgGridNg2;

AgGridNg2 没有暴露 nativeElement 或 ElementRef 属性。

我也试过了:

@ViewChild('logGrid') logGrid: ElementRef;

它没有 nativeElement 属性,因为它的类型是 AgGridNg2 而不是 ElementRef。

我需要 DOM 引用,因为 ag-grid doesn't expose scrollHeight 的内容。

【问题讨论】:

    标签: angular ag-grid ag-grid-angular


    【解决方案1】:

    您可以通过将ViewChild装饰器的read属性设置为ElementRef来获取对组件宿主元素的引用:

    @ViewChild('logGrid', { read: ElementRef }) logGridElementRef: ElementRef;
    

    【讨论】:

    • 这是一种享受!但是为什么 Visual Studio Code 继续抱怨“'AgGridAngular' 类型上不存在属性'nativeElement'”,即使(根据您的建议进行更改)我现在可以使用 nativeElement 值?
    猜你喜欢
    • 1970-01-01
    • 2020-06-14
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2020-06-28
    • 2017-01-24
    • 2019-04-14
    相关资源
    最近更新 更多