我想隐藏一个div并显示另一个div
My Code
我的问题是我在ngif中使用了多个条件,但是没有正常工作。一旦我点击“show sub content1”,想要隐藏主内容,想要显示“sub content1”,反之亦然。我该怎么办。请帮忙。
最佳答案
你刚刚接近你的结果,看看你的代码看起来你在学习,很好的工作!!您必须检查是否启用了任何一个内容,因此需要隐藏所有三个按钮并显示子内容。以下是符合您要求的正确代码,
<!-- Display all of the SubContents is disable. -->
<div *ngIf="!showSubContent && !showSubContentTwo && !showSubContentThree">
<button (click)="showSubContent=!showSubContent">Show Sub content1</button>
<button (click)="showSubContentTwo=!showSubContentTwo">Show Sub content2</button>
<button (click)="showSubContentThree=!showSubContentThree">Show Sub content3</button>
<h2> Main content </h2>
</div>
<!-- Display if SubContent-1 is enable. -->
<div *ngIf="showSubContent && !showSubContentTwo && !showSubContentThree">
Sub Content 1 here
<button (click)="showSubContent=!showSubContent">Show Main Content</button>
</div>
<!-- Display if SubContent-2 is enable. -->
<div *ngIf="showSubContentTwo && !showSubContent && !showSubContentThree">
Sub Content 2 here
<button (click)="showSubContentTwo=!showSubContentTwo">Show Main Content</button>
</div>
<!-- Display if SubContent-3 is enable. -->
<div *ngIf="showSubContentThree && !showSubContentTwo && !showSubContent">
Sub Content 3 here
<button (click)="showSubContentThree=!showSubContentThree">Show Main Content</button>
</div>