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

209
Vistas
class.active is not changing through (click) event handler

I'm trying to create a kind of toggle action when two buttons in angular.

Buttons to code

So, what should happen is when I click trending, burst should appear as inactive and vice versa. I'm trying to do this through [class.active] on angular:

<div class="col-6">
        <button [class.active]="burstButton" (click)="onTabChange()" clickable>Burst</button>
    </div>
    <div class="col-6">
        <button [class.active]="trendingButton"  (click)="onTabChange()" clickable>Trending</button>
    </div>

I tried to do this by declaring two boolean variables, one for each button:

public trendingButton: boolean = true;
public burstButton: boolean = false;

And handle the click event through a function obviously:

onTabChange() {

    console.log('Hello from ontabchange')
    if (this.trendingButton) {
      this.burstButton = false;
    } else {
      this.burstButton = true;
    }

}

My problem is, no matter what button I click, nothing happens. The log print on onTabChange appears in the console when I click either button so I don't understand what's going on.

Thank you in advance

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The logic in your onTabChange seems wrong. The value of this.trendingButton will always be true, and thus this.burstButton will always be assigned the false value from the first branch of your if-else.

Most importantly, have you defined the .active class in your CSS file? If not, then what you see is the expected behavior.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your condition should be like this...

trendingButton: boolean = false;
burstButton: boolean = false;
    
    onTabChange() {
        console.log('Hello from ontabchange')
        if (!this.trendingButton) {
          this.trendingButton= true;
        } else {
          this.trendingButton= false;
        }
if (!this.burstButton) {
          this.burstButton= true;
        } else {
          this.burstButton= false;
        }`enter code here`
    
    }

and please make sure that you have defined your "active" class in CSS

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have wrong logic in onTabChange and one extra variable. Make html code execute onTabChange with name of button:

<div class="col-6">
        <button [class.active]="currentButton === 'burst'" (click)="onTabChange('burst')" clickable>Burst</button>
    </div>
    <div class="col-6">
        <button [class.active]="currentButton === 'trending'"  (click)="onTabChange('trending')" clickable>Trending</button>
    </div>

int TS file:

public current: string = 'burst';
onTabChange(buttonName: string) {

    console.log('Hello from ontabchange')
    this.currentButton = buttonName;

}

Hope this helps.

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