【问题标题】:Switch the contents of "mat-cell" based on element's value根据元素的值切换“mat-cell”的内容
【发布时间】:2019-03-29 09:11:28
【问题描述】:

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

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

例子

<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>

【问题讨论】:

    标签: angular html-table angular-material2


    【解决方案1】:

    您可以使用 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>
    

    【讨论】:

      【解决方案2】:

      @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>
      

      【讨论】:

        猜你喜欢
        • 2013-08-21
        • 1970-01-01
        • 2019-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-12
        • 2021-11-04
        相关资源
        最近更新 更多