我有以下组件:

class MyComponent {
  public mode = 'v';
  readonly modes = ['v', 'a', 'd'];
  ....
}

现在,我想使用ngFor来显示modes中除当前存储在mode中的当前模式以外的所有模式的按钮。我有以下代码:
<button *ngFor="let othermode of modes">
  {{ othermode }}
</button>

我一直希望显示两个按钮,其中包含剩余的2种模式。我尝试了这个:
<button *ngFor="let othermode of modes.filter(elem => elem !== this.mode)">
  {{ othermode }}
</button>

但它不起作用。我看到的所有问题都需要为此功能编写自定义管道,但是没有仅使用值来过滤字符串数组的任何简单方法吗?

最佳答案

您可以使用 :

<ng-container *ngFor="let othermode of modes">
  <button *ngIf="othermode!=mode">
    {{ othermode }}
  </button>
</ng-container>

09-17 14:42