【发布时间】:2020-09-23 06:17:30
【问题描述】:
我遇到以下错误:
ERROR in Can't have multiple template bindings on one element. Use only one attribute prefixed with * (" </mat-header-cell>
<mat-cell *matCellDef="let order" class="{{orderColumn.fieldClass}}" [ERROR ->]*ngIf="orderColumn.fieldType == 'date'">{{order[orderColumn.fieldName] | date: globalDateFormat}}</ma")
Can't have multiple template bindings on one element. Use only one attribute prefixed with * ("ateFormat}}</mat-cell><mat-cell *matCellDef="let order" class="{{orderColumn.fieldClass}}" [ERROR ->]*ngIf="orderColumn.fieldType == 'text'">{{order[orderColumn.fieldName]}}</mat-cell>
下面是代码:
<mat-table #table [dataSource]="dataSource" matSort #sort1="matSort"
matSortActive="id" matSortDirection="asc" matSortDisableClear>
<!-- Checkbox Column -->
<ng-container matColumnDef="row">
<mat-header-cell *matHeaderCellDef class="mat-column-checkbox">
Row
</mat-header-cell>
<mat-cell *matCellDef="let row" class="mat-column-checkbox">
1
</mat-cell>
</ng-container>
<ng-container *ngFor="let orderColumn of newOrderColumns;" matColumnDef="orderColumn.fieldName">
<mat-header-cell *matHeaderCellDef mat-sort-header class="{{orderColumn.fieldClass}}">{{orderColumn.fieldLable}}
</mat-header-cell>
<mat-cell *matCellDef="let order" class="{{orderColumn.fieldClass}}" *ngIf="orderColumn.fieldType == 'date'">{{order[orderColumn.fieldName] | date: globalDateFormat}}</mat-cell>
<mat-cell *matCellDef="let order" class="{{orderColumn.fieldClass}}" *ngIf="orderColumn.fieldType == 'text'">{{order[orderColumn.fieldName]}}</mat-cell>
<mat-cell *matCellDef="let order" class="{{orderColumn.fieldClass}}" *ngIf="orderColumn.fieldType == 'Checkbox'">
<mat-checkbox (click)="$event.stopPropagation()"
[checked]="order[orderColumn.fieldName] == 'True'" [color]="'primary'">
</mat-checkbox>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
请帮我解决这个问题。
【问题讨论】:
-
尝试在这些单元格上使用
[hidden]而不是ngIf。 -
您不能同时使用 mafCellDef 和 ngIf,您可以在 ng-container 上使用 ngIf 并将 mat-cell 放在该容器内。
-
我同意@Piyush,使用
ng-container并将您的*ngIf放在那里。
标签: angular angular-material material-table