简体   繁体   English

如何在循环元素上使用 ngIf 语句

[英]How to use ngIf statement on a looping element

I have an angular material table:我有一个角材料表:

(HTML) (HTML)

<table mat-table
        [dataSource]="dataSource" multiTemplateDataRows
        class="mat-elevation-z8">
  <ng-container matColumnDef="{{column}}" *ngFor="let column of displayedColumns">
    <ng-container *ngIf="!column=='slaStatus'">
      <th mat-header-cell *matHeaderCellDef> {{column}} </th>
      <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
    </ng-container>
    <ng-container *ngIf="column=='slaStatus'" matColumnDef="{{column}}">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> SLA</th>
        <td mat-cell *matCellDef="let element">
            <mat-chip-list aria-label="Fish selection">
                <mat-chip *ngIf="element.slaStatus=='On Time'" style="background-color: #B9F6CA; color: green">On Time</mat-chip>
                <mat-chip *ngIf="element.slaStatus=='Late'" style="background-color: #B9F6CA; color: yellow">Late</mat-chip>
                <mat-chip *ngIf="element.slaStatus=='Missing'" style="background-color: #ec9d9d; color: red">Missing</mat-chip>
            </mat-chip-list>
        </td>
    </ng-container>
  </ng-container>

It is throwing an error它抛出一个错误

<ng-container *ngIf="!column=='slaStatus'">
error TS2367: This condition will always return 'false' since the types 'boolean' and 'string' have no overlap 

Basically I want to see if the column equals 'slaStatus' and if it does, I want to display it as an angular material chip with its own color.基本上,我想查看该列是否等于 'slaStatus',如果是,我想将其显示为具有自己颜色的有角材料芯片。 How can I write the ngIf condition in a way that checks to see if the column equals 'slaStatus'?如何以检查列是否等于“slaStatus”的方式编写 ngIf 条件?

(Typescript) (打字稿)

  displayedColumns: string[] = [
    'id',
    'tradingPartnerTradingPartner',
    'fileFormatFileFormat',
    'slaStatus',
  ];

 <table mat-table [dataSource]="dataSource" multiTemplateDataRows class="mat-elevation-z8"> <ng-container matColumnDef="{{column}}" *ngFor="let column of displayedColumns"> <ng-container *ngIf="column!=='slaStatus'"> <th mat-header-cell *matHeaderCellDef> {{column}} </th> <td mat-cell *matCellDef="let element"> {{element[column]}} </td> </ng-container> <ng-container *ngIf="column=='slaStatus'" matColumnDef="{{column}}"> <th mat-header-cell *matHeaderCellDef mat-sort-header> SLA</th> <td mat-cell *matCellDef="let element"> <mat-chip-list aria-label="Fish selection"> <mat-chip *ngIf="element.slaStatus=='On Time'" style="background-color: #B9F6CA; color: green">On Time</mat-chip> <mat-chip *ngIf="element.slaStatus=='Late'" style="background-color: #B9F6CA; color: yellow">Late</mat-chip> <mat-chip *ngIf="element.slaStatus=='Missing'" style="background-color: #ec9d9d; color: red">Missing</mat-chip> </mat-chip-list> </td> </ng-container> </ng-container>

it is a type cores-ion error.这是一个类型的核心离子错误。 *ngIf="column!=='slaStatus'" will be fine. *ngIf="column!=='slaStatus'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM