angular - 如何将图像添加到 Angular Material 表列中?

标签 angular angular-material

我正在使用 Angular Material 表。我想在该列中的标志文本之前有一个图标,或者如何添加带有图标的列?出于某种原因,我不知道如何做到这一点。到目前为止我的代码是:

组件.ts

export interface PeriodicElement {
  date: string;
  action: string;
  mileage: string;
  author: string;
  flag: string;
}

  const ELEMENT_DATA: PeriodicElement[] = [
  {date: 'xxxxx', action: 'xxxxx', mileage: "xxxxx", author:"xxxxx", flag:"Late"},
  {date: 'xxxxx', action: 'xxxxx', mileage: "xxxxx", author:"xxxxx", flag:"Late"},
  {date: 'xxxxx', action: 'xxxxx', mileage: "xxxxx", author:"xxxxx", flag:"Late"},
];

  displayedColumns: string[] = ['date', 'action', 'mileage', 'author', 'flag'];
  dataSource = new MatTableDataSource(ELEMENT_DATA);

HTML
<ng-container matColumnDef="flag">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> Flag</th>
        <td mat-cell *matCellDef="let element" class="status">{{element.flag}} </td>
</ng-container>

最佳答案

它就像向数据模型添加附加属性一样简单,例如icon :

export interface PeriodicElement {
  date: string;
  action: string;
  mileage: string;
  author: string;
  flag: string;
  icon: string;
}

将属性设置为您想要的任何图标(检查图标 here):
const ELEMENT_DATA: PeriodicElement[] = [
  {date: 'xxxxx', action: 'xxxxx', mileage: "xxxxx", author:"xxxxx", flag:"Late", icon: "flight_land"},
  {date: 'xxxxx', action: 'xxxxx', mileage: "xxxxx", author:"xxxxx", flag:"Late", icon: "flight_land"},
  {date: 'xxxxx', action: 'xxxxx', mileage: "xxxxx", author:"xxxxx", flag:"Late", icon: "flight_land"},
];

然后在您的 Flag 列中使用它:
<td mat-cell *matCellDef="let element" class="status"><mat-icon>{{element.icon}}</mat-icon>{{element.flag}}</td>

Here is a simple stackblitz based on the Angular Material table example with an icon displayed based on the property icon defined on the table data.

关于angular - 如何将图像添加到 Angular Material 表列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54457907/

相关文章:

css - 如何为 Angular Material 分页器自定义 css?

html - 文本上的 CSS NoWrap

angular - MatDialog 不显示

javascript - Angular 6 : error TS2339: Property 'value' does not exist on type 'HTMLElement'

Angular 9 延迟加载组件错误 : No provider for InjectionToken mat-autocomplete-scroll-strategy

css - Angular Material Design 如何垂直填充列

angular - 如何停止在自动完成组件中添加重复的芯片?

angular - ionic 网络类型 = null

angular - child 不呈现 parent 传递的模板

typescript - 将TypeScript编译成outDir时外部组件模板或css的路径