Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Angular switched buttons logic

I have a simple problem, but I'm a beginner so I can't understand the logic,

I have 2 buttons one and two, when I click on one I would like to display instead of one and two the other buttons so three and four? made a switch.

What is the best way to do this? Is using if else in the HTML correct or should we move this logic to the ts file?

Thanks in advance.

html

<div *ngIf="!isButtons">
  <button> one</button>
  <button> two</button>
</div>

<div>
  <button> three</button>
  <button> four</button>
</div>

ts.file

isButtons:boolean = false;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The logic is simple enough to remain in the template, you don't need any more code in your component. A simple (click) and a <ng-template> will do the trick:

<div *ngIf="!isButtons; else threeAndFour">
  <button (click)="isButtons=true"> one</button>
  <button> two</button>
</div>

<ng-template #threeAndFour>
    <div>
        <button> three</button>
        <button> four</button>
    </div>
</ng-template>
about 4 years ago · Juan Pablo Isaza Report

0

Given your code example, you can just

  • add *ngIf="isButtons" to the second div so you'll handle the final state
  • add the click handler on the button you want to trigger the change: (click)="isButtons = true"

Final snippet:

<div *ngIf="!isButtons">
  <button (click)="isButtons = true"> one</button>
  <button> two</button>
</div>

<div *ngIf="isButtons">
  <button> three</button>
  <button> four</button>
</div>

Jeremy Thille's answer is just as good, but most likely not something a beginner can handle (ng-template and more complex *ngIf usage).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!