首页 > 解决方案 > Angular Material mat-cell datepicker 将自定义 ID 传递给事件处理函数

问题描述

我在 mat-cell 中添加了一个 Angular Material mat-datepicker。When the datepicker is selected and the date is changed it will call a function in the typescript side. 我试图在 mat-table 绑定期间弄清楚如何将记录的 ID(主键)绑定到该 mat-cell 并将该值传递给事件处理函数。到目前为止,这是我的代码

<ng-container matColumnDef="ProcessDate">
            <mat-header-cell *matHeaderCellDef mat-sort-header><strong>{{columnName}}</strong></mat-header-cell>
            <mat-cell id={{mydata.id}} *matCellDef="let mydata"> 
                <input id={{mydata.id}} class="control-width" matInput [matDatepicker]="picker1" [value]="myData.date" (dateChange)="addEvent(id,  $event)">
                <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
                <mat-datepicker #picker1 color="primary"></mat-datepicker>
            </mat-cell> 
          </ng-container>

在打字稿方面,id 是“未定义的”。任何帮助表示赞赏。

标签: angularangular-material

解决方案


如果您想要的 id 是 mydata.id 属性,请将回调中的 id 更改为 mydata.id。该视图中没有 id 属性,除非您的组件上有名为 id 的属性,否则 id 将是未定义的。


推荐阅读