【问题标题】:Mat-icon display based on condition in for loop基于 for 循环条件的 Mat-icon 显示
【发布时间】:2021-12-15 10:37:01
【问题描述】:

我有一个 angular-django 应用程序,它对第三方应用程序进行 API 调用并返回 json 响应。我正在遍历响应并在表格中显示数据。

<tr *ngFor="let item of job_list">
     
      <td style="text-align:center">{{item?.ReleaseName}}</td>
      <td style="text-align:center">{{item?.Source}}</td>
      <td style="text-align:center"><mat-icon *ngIf="Successful" style="color:green;">check circle icon</mat-icon>
                                    <mat-icon *ngIf="Faulted" style="color: red;">error icon</mat-icon> 
                                    <mat-icon *ngIf="Stopped" style="color: grey;">cancel icon</mat-icon>
                                    {{item?.State}}
                                  
                                  
      </td>
      <td style="text-align:center">{{item?.StartTime | date:'dd-MM-yyyy         HH:mm:ss'}}</td>
      <td style="text-align:center">{{item?.EndTime | date:'dd-MM-yyyy           HH:mm:ss'}}</td>

     
     </tr>

在这里,我想根据 {{item?.State}} 值显示 mat-icon。例如如果值为Successful,我想显示“检查圆圈图标”,如果它是“Faulted”,我想显示“错误图标”等。这可能吗?

更新: 已实现 ngIf 但对齐无序:

谢谢

【问题讨论】:

标签: django angular angular-ng-if mat-icon


【解决方案1】:

1.您可以尝试ngSwitch 解决方案,但ngIf 也可以:

<td style="text-align:center">
    <mat-icon *ngIf="item?.State === 'Successful'" style="color:green;">check circle icon</mat-icon>
    <mat-icon *ngIf="item?.State === 'Faulted'" style="color: red;">error icon</mat-icon> 
    <mat-icon *ngIf="item?.State === 'Stopped'" style="color: grey;">cancel icon</mat-icon>
    {{item?.State}}                                                               
</td>

2。甚至可以使用返回 图标:

.html:

<td style="text-align:center">
    <mat-icon [class]="getIconByItemState(item, true)">{{ getIconByItemState(item) }}</mat-icon>
    {{item?.State}}                                                               
</td>

.ts:

getIconByItemState(item, color: boolean = false): string {
   switch(item?.State) { 
      case 'Successful': { 
         return !color ? 'check circle icon' : 'green-icon'; 
      } 
      case 'Faulted': { 
          return !color ? 'error icon' : 'red-icon'; 
      }
      case 'Stopped': { 
          return !color ? 'cancel icon' : 'grey-icon'; 
      } 
      default: { 
         return 'default icon'; // ? 
      } 
   }
}

1.最好的解决方案(如果可能的话)是让 State 变量名等于图标名:

<td style="text-align:center">
    <mat-icon [class]="item?.State + '-color'">{{ item?.State }}</mat-icon>
    {{item?.State}}                                                               
</td>

【讨论】:

  • 缺少放类的css,但基本上只有图标颜色
  • 谢谢。我实现了 ngIf(第一种方法),但是对齐有一些问题,可能是因为我们使用了很多 mat-icon 标签吗?我已为我的问题添加了屏幕截图。
  • 我需要更多信息,尝试使用开发人员工具来发现文本和图标之间是否存在某些元素,或者即使图标的大小不同。但是您可以尝试使用 css 对其进行调整,为每个图标赋予一个类,而不是定义 style="color: somecolor" 并添加对齐属性。更新:实际上,我猜这是因为您在图标和状态文本之间留下了空白。
  • 嗨,我解决了。这是因为我之前为图标定义了一个 css 类,而 mat-icons 使用的是该类的 css 设计。
【解决方案2】:
 <td style="text-align:center" [ngSwitch]="item.State">
            <mat-icon *ngSwitchCase="Successful" style="color:green;">check circle icon</mat-icon>
            <mat-icon *ngSwitchCase="Faulted" style="color: red;">error icon</mat-icon> 
           <mat-icon *ngSwitchCase="Stopped" style="color: grey;">cancel icon</mat-icon>
                                                {{item?.State}}
 </td>

【讨论】:

  • 您好,感谢您的快速回复。我实现了 ngSwitch 并得到了错误:core.mjs:10144 NG0303: Can't bind to '*ngSwitchCase' 因为它不是 'mat-icon' 的已知属性。
  • 因为是*ngSwitchCase 而不是**ngSwitchCase,不是吗?
  • *ngSwitchCase 抛出错误:错误 TS2339:“组件”类型上不存在属性“成功”。
  • &lt;mat-icon *ngSwitchCase="'Successful'" style="color:green;"&gt;check circle icon&lt;/mat-icon&gt;
  • 这行得通。谢谢!
猜你喜欢
  • 2020-08-08
  • 2014-09-12
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 2021-11-05
  • 2019-07-20
  • 1970-01-01
相关资源
最近更新 更多