You could have a responsible class for doing this, with a property for each div:
class SectionVisibilityHandler {
private boolean _div1 = false;
private boolean _div2 = false;
private boolean _div3 = false;
public toggle(idx: number): boolean {
this['_div' + idx] = !this['_div' + idx];
}
public get div1(): boolean {
return this._div1;
}
public get div2(): boolean {
return this._div2;
}
public get div3(): boolean {
return this._div3;
}
}
And then, access these properties using getters:
<div>
<div *ngIf="visibilityHandler.div1">
<div>One</div>
</div>
<button type="button" toggle()"></button>
</div>
<div>
<div *ngIf="visibilityHandler.div2">
<div>Two</div>
</div>
<button type="button" toggle()"></button>
</div>
<div>
<div *ngIf="visibilityHandler.div3">
<div>Three</div>
</div>
<button type="button" toggle()"></button>
</div>
And the visibility handler object is defined in your component properties:
public visibilityHandler: SectionVisibilityHandler = new SectionVisbilityHandler();
This way, you can customize your logic behind showing/hiding the div in a specific class and method.
However, you can define this class as a service and inject it inside the component.
NOTE: If your divs are dynamically generated then you need an array in the handling class and two methods, one for toggling a specific index and another one to access arrays' elements by index.
show(i.e. 1 variable), then changing it will affect everywhere it is bound. If you do not want to do that then you can go with the traditional javascript approach by sending the reference of the div and changing it's style.display tonone.