Angular 6 中 ngIf 中的多个条件
Multiple Conditions in ngIf in Angular 6
我想隐藏一个 div 并想显示另一个我在下面编写的代码
我的问题是我在 ngIf 中使用了多个条件但无法正常工作。单击 "Show sub content1" 后,我想隐藏主要内容并显示 "Sub Content 1",反之亦然。为此我该怎么办。请帮忙
您刚刚接近结果,查看您的代码看起来就像您在学习一样,干得好!!您必须检查是否启用了任何一个内容,因此您需要隐藏所有三个按钮并显示您的子内容。以下是根据您的要求正确的代码,
<!-- 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>
您可以使用 ngSwitch
来简化您的代码:
<div [ngSwitch]="showSubContent">
<div *ngSwitchDefault>
<button (click)="showSubContent=1">Show Sub content1</button>
<button (click)="showSubContent=2">Show Sub content2</button>
<button (click)="showSubContent=3">Show Sub content3</button>
<h2> Main content </h2>
</div>
<div *ngSwitchCase="1">
Sub Content 1 here
</div>
<div *ngSwitchCase="2">
Sub Content 2 here
</div>
<div *ngSwitchCase="3">
Sub Content 3 here
</div>
<button *ngIf="showSubContent" (click)="showSubContent=0">Show Main Content</button>
</div>
这样的作品不需要用那么多条件。这也不直观,无法理解您的逻辑,而是使用 switch
// Define a variable with name content in component
export class AppComponent {
content = 'mainContent'
constructor() {
}}
<div>
<button (click)="content='showSubContent'">Show Sub content1</button>
<button (click)="content='showSubContentTwo'">Show Sub content2</button>
<button (click)="content='showSubContentThree'">Show Sub content3</button>
</div>
<div [ngSwitch]="content">
<div *ngSwitchDefault>
My Main content
</div>
<div *ngSwitchCase="'showSubContent'">
Sub Content 1 here
<button (click)="content='showMainContent'">Show Main Content</button>
</div>
<div *ngSwitchCase="'showSubContentTwo'">
Sub Content 2 here
<button (click)="content='showMainContent'">Show Main Content</button>
</div>
<div *ngSwitchCase="'showSubContentThree'">
Sub Content 3 here
<button (click)="content='showMainContent'">Show Main Content</button>
</div>
</div>
我想隐藏一个 div 并想显示另一个我在下面编写的代码
我的问题是我在 ngIf 中使用了多个条件但无法正常工作。单击 "Show sub content1" 后,我想隐藏主要内容并显示 "Sub Content 1",反之亦然。为此我该怎么办。请帮忙
您刚刚接近结果,查看您的代码看起来就像您在学习一样,干得好!!您必须检查是否启用了任何一个内容,因此您需要隐藏所有三个按钮并显示您的子内容。以下是根据您的要求正确的代码,
<!-- 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>
您可以使用 ngSwitch
来简化您的代码:
<div [ngSwitch]="showSubContent">
<div *ngSwitchDefault>
<button (click)="showSubContent=1">Show Sub content1</button>
<button (click)="showSubContent=2">Show Sub content2</button>
<button (click)="showSubContent=3">Show Sub content3</button>
<h2> Main content </h2>
</div>
<div *ngSwitchCase="1">
Sub Content 1 here
</div>
<div *ngSwitchCase="2">
Sub Content 2 here
</div>
<div *ngSwitchCase="3">
Sub Content 3 here
</div>
<button *ngIf="showSubContent" (click)="showSubContent=0">Show Main Content</button>
</div>
这样的作品不需要用那么多条件。这也不直观,无法理解您的逻辑,而是使用 switch
// Define a variable with name content in component
export class AppComponent {
content = 'mainContent'
constructor() {
}}
<div>
<button (click)="content='showSubContent'">Show Sub content1</button>
<button (click)="content='showSubContentTwo'">Show Sub content2</button>
<button (click)="content='showSubContentThree'">Show Sub content3</button>
</div>
<div [ngSwitch]="content">
<div *ngSwitchDefault>
My Main content
</div>
<div *ngSwitchCase="'showSubContent'">
Sub Content 1 here
<button (click)="content='showMainContent'">Show Main Content</button>
</div>
<div *ngSwitchCase="'showSubContentTwo'">
Sub Content 2 here
<button (click)="content='showMainContent'">Show Main Content</button>
</div>
<div *ngSwitchCase="'showSubContentThree'">
Sub Content 3 here
<button (click)="content='showMainContent'">Show Main Content</button>
</div>
</div>