在 md-table 中实现 ng-if 指令
implementing ng-if directive inside md-table
我正在尝试在我的 md-table 中使用 ng-if,但出现错误:
Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with *
这是我的模板代码
<md-row *cdkRowDef="let row; columns: displayedColumns" (click)="viewNarrative(row)">
<md-cell *cdkCellDef="let row" *ngIf="!(authService.isAdmin())">
<button md-button (click)="viewNarrative(row)"><md-icon>toc</md-icon> Narrative</button>
<button md-button (click)="editDemographics(row)"><md-icon>edit</md-icon> Demographics</button>
<button md-button (click)="confirmDelete(row, $event)" style="line-height:normal"><md-icon>delete</md-icon> Delete</button>
</md-cell>
</md-row>
当我删除 *cdkCellDef="let row"
指令时,出现错误:ERROR Error: No provider for CdkColumnDef!
那么如何实现 ng-if
指令呢?
问题是您在一个元素上使用了两个带有星号语法的结构指令。您需要打开其中一个。以下应该有效:
<ng-template [ngIf]="!(authService.isAdmin())">
<md-cell *cdkCellDef="let row">
<button md-button (click)="viewNarrative(row)"><md-icon>toc</md-icon> Narrative</button>
<button md-button (click)="editDemographics(row)"><md-icon>edit</md-icon> Demographics</button>
<button md-button (click)="confirmDelete(row, $event)" style="line-height:normal"><md-icon>delete</md-icon> Delete</button>
</md-cell>
</ng-template>
或者只需将 ngIf
移动到 ng-container
:
<ng-container *ngIf="!(authService.isAdmin())">
<md-cell *cdkCellDef="let row">
你可以这样做:
这样您也可以拥有 mat-cell 和 *ngIf
<ng-container matColumnDef="isActive">
<mat-header-cell *matHeaderCellDef mat-sort-header> Active </mat-header-cell>
<mat-cell *matCellDef="let element"> <mat-icon *ngIf="element.isActive;else notactive;">done</mat-icon>
<ng-template #notactive><mat-icon>clear</mat-icon></ng-template>
</mat-cell>
</ng-container>
您可以像这样在 md-cell 外部应用 *ngIf。
<md-row *cdkRowDef="let row; columns: displayedColumns" (click)="viewNarrative(row)">
<md-cell *cdkCellDef="let row">
<button md-button (click)="viewNarrative(row)" *ngIf="!(authService.isAdmin())"><md-icon>toc</md-icon> Narrative</button>
<button md-button (click)="editDemographics(row)" *ngIf="!(authService.isAdmin())"><md-icon>edit</md-icon> Demographics</button>
</md-cell>
</md-row>
我正在尝试在我的 md-table 中使用 ng-if,但出现错误:
Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with *
这是我的模板代码
<md-row *cdkRowDef="let row; columns: displayedColumns" (click)="viewNarrative(row)">
<md-cell *cdkCellDef="let row" *ngIf="!(authService.isAdmin())">
<button md-button (click)="viewNarrative(row)"><md-icon>toc</md-icon> Narrative</button>
<button md-button (click)="editDemographics(row)"><md-icon>edit</md-icon> Demographics</button>
<button md-button (click)="confirmDelete(row, $event)" style="line-height:normal"><md-icon>delete</md-icon> Delete</button>
</md-cell>
</md-row>
当我删除 *cdkCellDef="let row"
指令时,出现错误:ERROR Error: No provider for CdkColumnDef!
那么如何实现 ng-if
指令呢?
问题是您在一个元素上使用了两个带有星号语法的结构指令。您需要打开其中一个。以下应该有效:
<ng-template [ngIf]="!(authService.isAdmin())">
<md-cell *cdkCellDef="let row">
<button md-button (click)="viewNarrative(row)"><md-icon>toc</md-icon> Narrative</button>
<button md-button (click)="editDemographics(row)"><md-icon>edit</md-icon> Demographics</button>
<button md-button (click)="confirmDelete(row, $event)" style="line-height:normal"><md-icon>delete</md-icon> Delete</button>
</md-cell>
</ng-template>
或者只需将 ngIf
移动到 ng-container
:
<ng-container *ngIf="!(authService.isAdmin())">
<md-cell *cdkCellDef="let row">
你可以这样做:
这样您也可以拥有 mat-cell 和 *ngIf
<ng-container matColumnDef="isActive">
<mat-header-cell *matHeaderCellDef mat-sort-header> Active </mat-header-cell>
<mat-cell *matCellDef="let element"> <mat-icon *ngIf="element.isActive;else notactive;">done</mat-icon>
<ng-template #notactive><mat-icon>clear</mat-icon></ng-template>
</mat-cell>
</ng-container>
您可以像这样在 md-cell 外部应用 *ngIf。
<md-row *cdkRowDef="let row; columns: displayedColumns" (click)="viewNarrative(row)">
<md-cell *cdkCellDef="let row">
<button md-button (click)="viewNarrative(row)" *ngIf="!(authService.isAdmin())"><md-icon>toc</md-icon> Narrative</button>
<button md-button (click)="editDemographics(row)" *ngIf="!(authService.isAdmin())"><md-icon>edit</md-icon> Demographics</button>
</md-cell>
</md-row>