Angular 如何检索角度材质表中选定行的值

Angular 如何检索角度材质表中选定行的值,angular,Angular,我是一名初学者,我有一张垫桌: <table mat-table [dataSource]="list_equipment" matSort style="width: 100%;"> <!-- id Column --> <ng-container matColumnDef="id"> <th mat-header-cell *matHeaderCellDef s

我是一名初学者,我有一张
垫桌

<table mat-table [dataSource]="list_equipment" matSort style="width: 100%;"> 
    <!-- id Column -->
    <ng-container matColumnDef="id">
      <th mat-header-cell *matHeaderCellDef style="text-align: center !important;" mat-sort-header> id </th>
      <td mat-cell *matCellDef="let list_equipment" style="text-align: center;"> {{list_equipment.id}} </td>
    </ng-container>
     <!-- CaptionCode Column -->
     <ng-container matColumnDef="Caption">
      <th mat-header-cell *matHeaderCellDef style="text-align: center !important;" mat-sort-header> caption</th>
      <td mat-cell *matCellDef="let list_equipment" style="text-align: center;"> {{list_equipment.Caption}} </td>
    </ng-container>
    ...
    <tr mat-header-row *matHeaderRowDef="displayedColumnsequipment"></tr>
    <!-- <tr mat-row *matRowDef="let row; columns: displayedColumnsequipment;" (click)="highlight(row)" [ngClass]="{'highlightTableColor': selectedRowIndex == row.position}"></tr> -->
    <tr mat-row class="table-row" tabindex="1" *matRowDef="let row; columns: displayedColumnsequipment;"  (click)="highlight(row)"></tr>
  </table>
  <mat-paginator [pageSizeOptions]="[10, 20, 50]" showFirstLastButtons></mat-paginator>

问题是,无论我在html代码中使用
{{selectedRow.id}}
,控制台中都会出现此错误:
无法读取未定义的属性“id”
您正在定义未初始化的值,因此必须将该值设置为“安全导航运算符”?”


因此,此条件不会中断代码,也不会为您提供未定义或空值。

发生这种情况是因为属性
selcetedRow
未初始化,可以为空。所以你应该考虑这个案子。简单的方法是
{{selectedRow?.id}
highlight(row){
  this.selectedRow=row;
  console.log(this.selectedRow); 
}
                 {{selectedRow.id}} to {{selectedRow?.id}}