1

I want to use some Li list to show/hide multiple divs angular2.

The page will initially show all divs. when the small screen i hide some div in In small screen when i click list 1 how to show the particular Div

<li><i class="fa fa-location-arrow" aria-hidden="true" (click)="showDiv(1)"></i>Locator</li>
<li><i class="fa fa-gift" aria-hidden="true" (click)="showDiv(2)"></i>Offer</li>
<li><i class="fa fa-phone" aria-hidden="true" (click)="showDiv(3)"></i> Contact</li>
<li><i class="fa fa-calendar" aria-hidden="true" (click)="showDiv(4)"></i>Holiday</li>
<li><i class="fa fa-question-circle-o" aria-hidden="true" (click)="showDiv(4)"></i>FAQ</li>

<div id="div1" *ngIf="windowWidth > 767">Lorum Ipsum</div>
<div id="div2" *ngIf="windowWidth > 767">Lorum Ipsum</div>
<div id="div3" *ngIf="windowWidth > 767">Lorum Ipsum</div>
<div id="div4" *ngIf="windowWidth > 767">Lorum Ipsum</div>

file.ts:

windowWidth: number = window.innerWidth;

  //initial values, The window object may still be undefined during this hook, let me know if that's the case and we'll figure out a better hook for the initial value
  ngAfterViewInit() {
      this.windowWidth = window.innerWidth;
  }

  //if screen size changes it'll update
  @HostListener('window:resize', ['$event'])
  resize(event) {
      this.windowWidth = window.innerWidth;
  }

1 Answer 1

6
class MyComponent {
  selectedIndex = -1;

  showDiv(index) {
    this.selectedIndex = index;
  }
}
<li><i class="fa fa-location-arrow" aria-hidden="true" (click)="showDiv(1)"></i>Locator</li>
<li><i class="fa fa-gift" aria-hidden="true" (click)="showDiv(2)"></i>Offer</li>
<li><i class="fa fa-phone" aria-hidden="true" (click)="showDiv(3)"></i> Contact</li>
<li><i class="fa fa-calendar" aria-hidden="true" (click)="showDiv(4)"></i>Holiday</li>
<li><i class="fa fa-question-circle-o" aria-hidden="true" (click)="showDiv(4)"></i>FAQ</li>

<div id="div1" *ngIf="windowWidth > 767 && selectedIndex === 1">Lorum Ipsum</div>
<div id="div2" *ngIf="windowWidth > 767 && selectedIndex === 2">Lorum Ipsum</div>
<div id="div3" *ngIf="windowWidth > 767 && selectedIndex === 3">Lorum Ipsum</div>
<div id="div4" *ngIf="windowWidth > 767 && selectedIndex === 4">Lorum Ipsum</div>
Sign up to request clarification or add additional context in comments.

2 Comments

ok after hide how can i show particular Div when i click event? like jsfiddle.net/XwN2L
I updated my answer. If you want to show/hide a single div, ng-container is no help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.