angular - 将附加数据附加到角度组件
问题描述
我有几个动态生成的MatTable组件(由 *ngFor):
<div *ngFor="....">
<table mat-table [attr.data-table-id]="someId" >
....
</table>
.....
</div>
现在,在组件的类文件中,我想找到一个特定的 MatTable 参考。我在组件类中定义了一个属性:
@ViewChildren(MatTable) tables: QueryList<MatTable>;
现在,我参考了所有的 MatTables。但我想找到一个特定的 MatTable,例如具有特定属性的 MatTable(例如 data-table-id = 12)。如何访问每个 MatTable 的对应 DomElement?如果我可以获得对组件的相应 DomElement 的引用,我可以读取该元素的属性并找到我想要的组件(MatTable)。
解决方案
您可以使用基本的 HTML id 标记,并通过以下方式访问它:
<table mat-table id="myId" [attr.data-table-id]="someId" >
....
</table>
在组件中:
@ViewChildren(MatTable, {read: ElementRef}) public tables: QueryList<ElementRef>;
let theTableIWant: ElementRef = this.tables.find((el: ElementRef) => el.nativeElement.id === myId);
推荐阅读
- node.js - 在单个 Docker 容器下具有多个服务器的节点应用程序对于某些服务器/端口返回 404
- reporting-services - 选择了多个值的级联参数仅显示顶部的一个
- aws-lambda - Jmeter replay for-aws-cognito-login 的问题
- spring-boot - 使用身份验证服务器作为尤里卡客户端从资源服务器访问身份验证服务器时出错
- python - 如何使用 Python 在没有 GCS 中的路由的情况下获取子文件夹中的文件名?
- java - 如何通过 JMX 在 WebSphere Liberty 中创建数据源
- python - Python中3D势场的平滑/插值
- python - 如何检查python中的xml标记值是否为none
- sql - 如果缺少表而不是抛出“无效的对象名称”错误,如何强制 SQL 语句执行不同的选择?
- r - Dplyr 对分组中的值进行计算,将每个项目与组中的所有 _other_ 项目进行比较