1

I am started to learn angular 4. I have multiple div section, each div section have button and content, once click the button clicked then will show the respective div, once click another button hide others.

<div *ngFor="let number of [0,1,2,3,4] ; let i = index">
  <div>
    <button (click)="showContent(this);"> Click Me {{index}} </button
    <div class="content"> Hello world {{index}}</div>
  </div>      
</div>

Js code

export class AppComponent {

  showContent(evt){

    event.target.style.display = 'block';   
  }   

}

Sorry, I added minimal code only.

Kamil Naja
  • 6,267
  • 6
  • 33
  • 47
RSKMR
  • 1,812
  • 5
  • 32
  • 73

4 Answers4

0

You can use <div *ngIf="variable"> with variable being a boolean. Your function showContent() can change variable to true and your component will be shown.

Pierre R-A
  • 509
  • 9
  • 13
0

There are different ways to solve this problem:

1- creating another array that reserves the state of each item, which I don't like :

public state = ['none','none','none','none','none'];  


  <div *ngFor="let number of [0,1,2,3,4] ; let i = index">
    <div>
      <button (click)="state[i]='block'"> Click Me {{index}} </button
      <div [style.display]='state[i]' class="content"> Hello world {{index}}</div>
    </div>      
  </div>

2- Creating a directive that encapsulates the logic and it's reusable :

@Directive({

   selector:`[showHide]`,
   exportAs :'showHide'

})

export class ShowHideDirective{

  @Input('showHide') isHidden = true; // initial state


  public show(){

       this.isHidden = false;
  }



  @HostBinding('[atrr.hidden]') get isItHidden() { // if this is not working for you, you can change it to `[style.display]` and return the `block` or `none`
     return this.isHidden;
  };


}


<div *ngFor="let number of [0,1,2,3,4] ; let i = index">
  <div>
    <button (click)="showHideElement.show()"> Click Me {{index}} </button
    <div #showHideElement='showHide' class="content"> Hello world {{index}}</div>
  </div>      
</div>

Don't forget to declare ShowOnClickDirective in your app module.

Milad
  • 27,506
  • 11
  • 76
  • 85
  • there is a difference though, display:none wont remove the node from the DOM – Jota.Toledo Jan 06 '18 at 13:09
  • @Jota.Toledo yep, but I can't see if he want's to remove it or simply hide it, based on his code snippet he wants to hide it – Milad Jan 06 '18 at 14:42
  • @Milad, Once click the "Click Me" button its showing the respective content, but the same time needs to hide other contents. Its possible? – RSKMR Jan 08 '18 at 05:11
  • @RSKMR send me the github, I'll code it for ya – Milad Jan 08 '18 at 05:27
0

This might help:

<div *ngFor="let number of [0,1,2,3,4] ; let i = index">
  <div>
    <button (click)="showContent(this, index);"> Click Me {{index}} </button
    <div class="content" *ngIf="selectedIndex == index"> Hello world {{index}}</div>
  </div>      
</div>

JS:

export class AppComponent {
  selectedIndex = -1;
  showContent(evt, index) {
    this.selectedIndex = index;   
  }
}
Pratyush Sharma
  • 257
  • 2
  • 7
0

This might help:

<ng-container *ngFor="let item of [1,2,3,4];let i=index">
    <div style="height:40px;border:1px solid #000;margin-bottom:10px">
        <button (click)="selectedDivIndex=i">Button {{i+1}}</button>
        <div class="content" *ngIf="selectedDivIndex===i">
            Content for #{{i+1}}
        </div>
    </div>
</ng-container>

Note: Set selectedDivIndex to some default index in the component file(.ts) if you want to load content initially

Ashraful Islam
  • 1,228
  • 10
  • 13