0

I'm tried to hide Angular typescript current div and show another div but its not working correctly dose anyone know some solution?

here the code

html

<div class="m-t-10">
  <div >

    <div class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 1 </p>
    </div>
  </div>


  <div *ngIf="showSelected">

    <div class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 2 </p>
    </div>
  </div>

.ts

showSelected: boolean;    

constructor() {
  this.showSelected = false;
}

ToggleButton() {
  this.showSelected = !this.showSelected;
}
2
  • 1
    Your component 1 div should have *ngIf="!showSelected" Commented Apr 25, 2022 at 8:56
  • 1
    CSS display:none for one of the classes and display: block for the other then JS to toggle two classes DOMElement.classlist.toggle(1stclass, 2ndclass)? Could that work Commented Apr 25, 2022 at 9:59

2 Answers 2

2

You can use a simple ngIf on both component wrappers, or ngIf with ngTemplate. When you have more than 2 possible components you can use a ngSwitch

  1. ngIf example
  <div *ngIf="!showSelected">
    <div class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 1 </p>
    </div>
  </div>


  <div *ngIf="showSelected">
    <div class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 2 </p>
    </div>
  </div>
  1. ngIf with ngTemplate example
  <div>
    <div *ngIf="!showSelected; else otherComponent" class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 1 </p>
    </div>
  </div>


  <ng-template #otherComponent>
    <div class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 2 </p>
    </div>
  </div>
  1. ngSwitchCase example
  <div [ngSwitch]="selectedComponentName">
    <div *ngSwitchCase="'component1'" class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 1 </p>
    </div>
    <div *ngSwitchCase="'component2'" class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 2 </p>
    </div>
    <div *ngSwitchCase="'component3'" class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
      <p> component 3 </p>
    </div>
  </div>
Sign up to request clarification or add additional context in comments.

Comments

0

Your HTML should be like:

<div *ngIf="!showSelected">
<div class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
  <p> component 1 </p>
</div>
<div *ngIf="showSelected">
<div class="circle-us"  nzTooltipTitle="United States" nzTooltipPlacement="top"  nz-tooltip (click)="ToggleButton()">
  <p> component 2 </p>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.