Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda