【问题标题】:Can't have multiple template bindings on one element. Use only one attribute prefixed with * ("</mat-header-cell>一个元素上不能有多个模板绑定。仅使用一个以 * ("</mat-h​​eader-cell> 为前缀的属性
【发布时间】: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


【解决方案1】:

您不能在一个元素上拥有多个模板绑定。 *matCellDef 和 *ngIf 是结构指令,不能在 mat-cell 中一起使用。

您可以通过使用 span 或任何其他元素包装元素来构建相同的内容。例如,你可以试试这个:

<ng-container *ngIf="orderColumn.fieldType == 'date'">
    <mat-cell *matCellDef="let order" class="{{orderColumn.fieldClass}}"> 
    {{order[orderColumn.fieldName] | date: globalDateFormat}}</mat-cell>
</ng-container>

https://angular.io/guide/structural-directives

【讨论】:

  • 使用 ng-container 是个好主意,因为它不会呈现为 html 但可以解决问题。
猜你喜欢
  • 2019-05-04
  • 1970-01-01
  • 2017-04-23
  • 2019-08-02
  • 2011-09-01
  • 2020-11-16
  • 1970-01-01
  • 2012-05-28
相关资源
最近更新 更多