-1

I have the following using Angular 7:

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

I get the following error:

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

How can I conditionally render a LI item in a loop?

Miguel Moura
  • 32,822
  • 74
  • 219
  • 400

4 Answers4

3

You can only one structural directive on an element to Change your code to

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

</ul>

Working stackblitz

jitender
  • 8,454
  • 1
  • 17
  • 43
2

separate the ngIf and ngFor. You can't have 2 structured directives on one element

<ul>
  <ng-template  *ngFor="let status of statuses$ | async" > 
    <li *ngIf="status.name != post.statusName">
     {{status.name}}
     </li>
  </ng-template>
</ul>
Sachila Ranawaka
  • 31,266
  • 5
  • 51
  • 75
1

Try this,

 <ul>
 <li *ngFor="let status of statuses$ | async" >
 <div *ngIf="status.name != post.statusName">
  <p>{{status.name}}</p>
 </div>
 </li>
 </ul>
Dixit Savaliya
  • 573
  • 2
  • 7
0

We Can't use ngif and ngfor together

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