Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
How to remove class "show" from previous li in angular?

I have a question, how to remove class "show" from previous li when the user clicks another menu in angular?

I've tried using the directive, but the "show" class still can't be removed from the previous li. This is the directive I have made :

import { Directive, ElementRef, HostListener, Renderer2 } from "@angular/core";

@Directive({
  selector: '[appDropdown]'
})

export class DropdownDirective {
  manageDropdown : boolean = false;

  constructor(private elementRef: ElementRef, private renderer: Renderer2)   {}

  @HostListener('click') openDropdown() {
    if(!this.manageDropdown) {
      this.renderer.addClass(this.elementRef.nativeElement,'show');
      this.manageDropdown = !this.manageDropdown;
    } else {
      this.renderer.removeClass(this.elementRef.nativeElement, 'show');
      this.manageDropdown = !this.manageDropdown;
    }
  }
}

And here's the appDropdown selector that I call and use in the HTML file

<ul class="navbar-nav">
    <li class="nav-item dropdown" appDropdown></li>
    <li class="nav-item dropdown" appDropdown></li>
    <li class="nav-item dropdown" appDropdown></li>
    <li class="nav-item dropdown" appDropdown></li>
</ul>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can get the parentElement.childNodes to get the list of all childNode of the parent of the currently clicked element.

Add the below to your openDropdown Hostlistener.

for(let child of this.elementRef.nativeElement.parentElement.childNodes){
      this.renderer.removeClass(child, 'show');
 }

Final Code:

@HostListener('click') openDropdown() {
    for(let child of this.elementRef.nativeElement.parentElement.childNodes){
      this.renderer.removeClass(child, 'show');
    }
    
    if(!this.manageDropdown) {
      this.renderer.addClass(this.elementRef.nativeElement,'show');
      this.manageDropdown = !this.manageDropdown;
    } else {
      this.renderer.removeClass(this.elementRef.nativeElement, 'show');
      this.manageDropdown = !this.manageDropdown;
    }
  }
about 4 years ago · Juan Pablo Isaza Relatório

0

I would probably move the responsibility of opening/closing the dropdowns one level higher. Meaning the component that renders the navbar would know the currently open menu, since as I understand you only want to have one open at a time.

Another solution would be to process the arguments of click HostListener, and compare the target to your current nav-item element. If its different, you can remove the class.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda