Search code examples
htmlangularenumsangular-ng-if

How to show elements of a list using `<*ngIf>` and enum variable


I have a list mad with angular, and I want to show elements of this list, with an enum variable. For example if enum = 0 than it shows all elements, if enum = 1 it shows only elements with list.status = true and if enum = 2 it shows only elements wiht list.status = false.

<ul class="list" style="list-style-type:none">
    <div>
      <li *ngFor="let list of currentList">
        <div>
          <input type="checkbox" class="stat" (click)="status(list)" [(checked)]="list.status" />
          <span class="task">{{list.task}}</span>
          <button class="listBtn" (click)="edit(list)">EDIT</button>
          <button class="listBtn" (click)="remove(list)">REMOVE</button>
        </div>
      </li>
    </div>
  </ul>

Solution

  • You would need a method which returns a boolean after doing the check, something like

    public checkEnum( list: any ): boolean {
      if ( enum == 0 ) {
       return true;
      }
      if ( enum == 1 && list.status == true ) {
       return true;
      }
      if ( enum == 2 && list.status == false ) {
       return true;
      }
      return false;
    }
    

    and then in the template:

        <div *ngIf="checkEnum(list)">
          <input type="checkbox" class="stat" (click)="status(list)" [(checked)]="list.status" />
          <span class="task">{{list.task}}</span>
          <button class="listBtn" (click)="edit(list)">EDIT</button>
          <button class="listBtn" (click)="remove(list)">REMOVE</button>
        </div>
    

    See if it helps.