简体   繁体   English

Angular 6中ngIf的多个条件

[英]Multiple Conditions in ngIf in Angular 6

I want to hide one div and want to show another for that I have written below code 我想隐藏一个div,并希望显示另一个div,因为我已经编写了下面的代码

My Code 我的守则

My problem is I have used multiple conditions in ngIf but not working properly. 我的问题是我在ngIf中使用了多个条件但是没有正常工作。 Once I click on "Show sub content1" want to hide main content and want to show "Sub Content 1" and vise versa for all buttons. 一旦我点击“显示子内容1”想隐藏主要内容并希望显示“子内容1”,反之亦然所有按钮。 What should I do for this. 我该怎么做呢 Please help. 请帮忙。

Your were just near to your result and looking into your code looks like you are learning, Good Work!! 你刚刚接近你的结果,看着你的代码看起来像你正在学习,好工作! you have to check just if any one of the content is enable so you need to hide All of the three buttons and display your Sub contents. 您必须检查是否启用了任何一个内容,因此您需要隐藏所有三个按钮并显示您的子内容。 Below is the correct code as per your requirement, 根据您的要求,下面是正确的代码,

<!-- 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>

You could simplify your code using ngSwitch : 您可以使用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>

There is no need to use so many conditions to make work like this. 没有必要使用这么多条件来完成这样的工作。 This is not intutive as well to understand your logic instead use 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM