angular - 角度,通过传递的引用访问本机元素
问题描述
我可以将 ElementRef 传递给子组件,然后在那里访问它的 nativeElement 吗?我得到了 html 元素,但我无法访问 nativeElement。我可以做些什么来访问 nativeElement 或如何直接传递它?
父模板:
<tr #refTr1>
<app-cell [parentRef]="refTr1"></app-cell>
</tr>
子组件“appCellComponent”:
@Input parentRef: ElementRef;
ngOnInit(): void {
console.log(this.parentRef.nativeElement.classList);
}
解决方案
你在 @Input 装饰器之后忘记了 ()
并且您的属性类型是 HTMLTableRowElement;
@Input() parentRef: HTMLTableRowElement;
而且你不需要在this.parentRef之后使用nativeElement
推荐阅读
- vitess - 是否可以使用辅助分片密钥对 Visess 进行分片
- ios - 如何使用 addObserver 闭包方法在 Swift 5 中删除Observer
- docker - gcloud:在将 docker 容器部署到 GCE 时添加 docker run 参数
- python - 交换图例中的一些标签 - matplotlib
- javascript - 如何在Javascript中的每次迭代之间设置超时?
- debugging - Tomcat 调试错误:无法打开调试器端口 (127.0.0.1:63199):java.net.BindException “地址已在使用:NET_Bind”
- sql - 获取函数中的表名
- javascript - 如何在反应导航 5 中定义屏幕内的自定义标题?
- python - 如何在Python中使用`-n`参数运行os命令`echo`
- orbeon - 将自动完成字段迁移到动态下拉列表