根据元素的值切换“ mat-cell”的内容

时间:2018-10-24 20:48:17

标签: angular html-table angular-material2

有什么方法可以检查元素属性的值(在这种情况下 element.VerificationCode),并以此为基础切换单元格的内容?

我需要在单元格中显示VerificationCode,如果element.VerificationCode为null,则显示按钮以生成一个。

示例

<ng-container matColumnDef="VerificationCode">
  <th mat-header-cell *matHeaderCellDef mat-sort-header> Family code </th>
     <td mat-cell *matCellDef="let element" (click)="$event.stopPropagation()
       <!-- 1 -->
         {{element.VerificationCode}}
       <!-- 2 -->        
         <button  mat-stroked-button (click)="genVerificationCode(group.id)">
             Generate 
         </button>         
 </td>
</ng-container>

2 个答案:

答案 0 :(得分:1)

@TeddySterne版本的替代方法(我相信这是在最新版本的Angular中首选的方式,但我可能是错的):

<td mat-cell *matCellDef="let element" (click)="$event.stopPropagation()">
  <ng-container *ngIf="element.VerificationCode">{{element.VerificationCode}}</ng-container>
  <button *ngIf="!element.VerificationCode" mat-stroked-button (click)="genVerificationCode(group.id)">Generate</button>        
</td>

答案 1 :(得分:0)

您可以将ngIf与else语句一起使用来完成它。示例:

<td mat-cell *matCellDef="let element" (click)="$event.stopPropagation()">
 <span *ngIf="element.VerificationCode; else showButton">{{element.VerificationCode}}</span>
 <ng-template #showButton>
   <button mat-stroked-button (click)="genVerificationCode(group.id)">
       Generate 
   </button>  
 </ng-template>       
</td>