在 For 循环中有条件地呈现 LI 项

Conditionally render a LI item in a For loop

我有以下使用 Angular 7:

<ul>
  <li *ngFor="let status of statuses$ | async" *ngIf="status.name != post.statusName">
    {{status.name}}
  </li>
</ul>

我收到以下错误:

Can't have multiple template bindings on one element. Use only one attribute prefixed with *

如何在循环中有条件地呈现 LI 项?

ngIfngFor 分开。一个元素上不能有 2 个结构化指令

<ul>
  <ng-template  *ngFor="let status of statuses$ | async" > 
    <li *ngIf="status.name != post.statusName">
     {{status.name}}
     </li>
  </ng-template>
</ul>

您只能将一个元素上的一个结构指令更改为

<ul>
<ng-container *ngFor="let status of statuses$ | async">
 <li  *ngIf="status.name != post.statusName">
    {{status.name}}
  </li>
</ng-container>

</ul>

Working stackblitz

试试这个,

 <ul>
 <li *ngFor="let status of statuses$ | async" >
 <div *ngIf="status.name != post.statusName">
  <p>{{status.name}}</p>
 </div>
 </li>
 </ul>

我们不能同时使用 ngif 和 ngfor

        <ul>
          <li *ngFor="let status of statuses$ | async" >
            <div *ngIf="status.name != post.statusName">
              <span>{{status.name}}</span>
            </div>
          </li>
        </ul>