0

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
  • 36,732
  • 85
  • 259
  • 481

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
  • 10,238
  • 1
  • 18
  • 44
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
  • 39,756
  • 7
  • 56
  • 80
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
  • 413
  • 3
  • 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>
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43