当我在 angular 中使用 innerHtml 时,mat-icon 元素不显示图标文本而不是图标形状

mat-icon element doesn't shows icon text instead of icon shape when I use innerHtml in angular

我想通过 innerHtml 将 mat-icon 动态添加到我的组件,但结果是图标文本而不是图标形状。

这是我的模板变量:

edit_template = `
  <button mat-icon-button color="primary" (click)="clicked()">
    <mat-icon aria-label="Edit data">edit</mat-icon>
  </button>
`;

这里是 innerHtml:

<table mat-table [dataSource]="this.dataSource">
  <div *ngFor="let displayedColumn of displayedColumns">
    <ng-container [matColumnDef]="displayedColumn">
      <th mat-header-cell *matHeaderCellDef> {{displayedColumn}} </th>
      <td mat-cell *matCellDef="let element" [innerHTML]="element[displayedColumn]"></td>
    </ng-container>
  </div>
</table>

结果是 edit 单词而不是编辑图标!

除了提到的问题,甚至 edit word 也不是按钮而是文本!

你有什么想法?

使用这种方式,你不需要编辑 HTML

<mat-icon>{{element[displayedColumn]}}</mat-icon>

我终于成功了!

这是我的解决方案:

修改定义:

edit_template = `edit`;

并且在 html 文件中我使用了 ngIf:

<table mat-table [dataSource]="this.dataSource">
  <div *ngFor="let displayedColumn of displayedColumns">
    <ng-container [matColumnDef]="displayedColumn">
      <th mat-header-cell *matHeaderCellDef> {{displayedColumn}} </th>
      <td mat-cell *matCellDef="let element">
          <div *ngIf="element[displayedColumn] === 'edit' || element[displayedColumn] === 'delete';then content else other_content">here is ignored</div>
          <ng-template #content>
            <button mat-icon-button color="primary">
              <mat-icon>{{element[displayedColumn]}} </mat-icon>
            </button>
          </ng-template>
          <ng-template #other_content>{{element[displayedColumn]}} </ng-template>
      </td>
    </ng-container>
  </div>
</table>

结果如我所料