根据元素的值切换 "mat-cell" 的内容
Switch the contents of "mat-cell" based on element's value
有没有办法检查元素的 属性 的值(在这种情况下
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>
您可以使用带有 else 语句的 ngIf
来完成它。示例:
<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>
替代@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>
有没有办法检查元素的 属性 的值(在这种情况下 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>
您可以使用带有 else 语句的 ngIf
来完成它。示例:
<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>
替代@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>