Hide element using ngFor and ngIf

hafizi hamid

I want to hide the other 3 elements in li element after the one of the list was clicked (the clicked list remain unhide), as I try it hide all the li element.

payment.component.ts

  paymentLists = [
    {
      name: 'IpayEasy',
    },
    {
      name: 'Credit Card',
    },
    {
      name: 'Internet Banking',
    },
    {
      name: '7-Eleven',
    },
  ];

  selectedIndex: number;

  select(index:number) {
    this.selectedIndex = index;
  }

payment.component.html

  <ul *ngIf="selectedIndex == paymentList">
    <li (click)="select(paymentList)" 
      *ngFor="let paymentList of paymentLists; let i=index">
      <span>{{paymentList.name}}</span>
    </li>
  </ul>

Here what have I tried, demo

Before:

  • IpayEasy
  • Credit Card
  • Internet Banking
  • 7-Eleven (clicked li)

    After:

  • 7-Eleven (li element remain unhide)

  • Nikhil Aggarwal

    You need to update your template as following

    • Move ngFor to ng-container element
    • Update ngIf condition to be true only if there is no selected index or the matching selected index
    • Pass index in select function

    Updated html will be as follows

    <ul>
       <ng-container *ngFor="let paymentList of paymentLists; let i=index" >
           <li (click)="select(i)"  *ngIf="selectedIndex === undefined || selectedIndex == i" [ngClass]="{'tab__list--selected--mobile': selectedIndex == paymentList}">
              <span>{{paymentList.name}}</span>
           </li>
       </ng-container>
    </ul>
    

    For reference, here is the working version

    Collected from the Internet

    Please contact [email protected] to delete if infringement.

    edited at
    0

    Comments

    0 comments
    Login to comment

    Related

    Angular 2 with *ngFor and ngIf to hide first element

    How to hide/show a current element (using *ngfor)

    Using ngIf and ngFor in option

    *ngIf and *ngFor on <td></td> element

    Angular 2 hide and show element using *ngIf with boolean

    Hide element if in the same date and only show it once using ngIf

    Using NgFor in an NgIf and NgIfElse block

    *ngIf and *ngFor on same element causing error

    *ngFor and *ngIf on a <tr> element in Angular 5

    Hide an HTML element using *ngIf when a user clicks outside of the specific element (Angular)

    Why when I'm using `*ngIf` and `*ngFor` on same element, `*ngFor` add null item to array which I'm iterating?

    In Angular, using *ngFor inside *ngIf not working

    Comma separated list using angular ngfor and ngif

    Apply a condition using ngFor and ngIf in angular 2

    Angular - Using *ngIf with *ngFor to specific rows in table

    Using *ngFor with ng-container and *ngIf

    Angular *ngIf Condition inside *ngFor to show hide items

    Angular How to use *ngIf and *ngFor on same Html element

    Angular2 - *ngFor and *ngIf on same element producing error

    How to use variables used in *ngFor/*ngIf to fill the html element properties

    Error in *ngIf on one element not letting *ngFor to work properly on another

    Preselected Element when using *ngFor

    angular 6 + show hide element with ngIf doesn't work

    Angular - Conditional formatting a label with ngIf and ngFor using regex and css

    Angular - primeng ngfor multiple conditions for the same columns using ngif

    How to display 4 values in every row in html using ngfor and ngif?

    Using ngIf without an extra element in Angular 2

    Ho to use ngIf in a *ngFor mat table to hide columns/truncate cell content

    ngFor and ngIf not working