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

389
Visualizações
sort the menu alphabetically in angular

am trying to figure out if there's a to sort the items(buttons) inside the menu in alphabetical order by means of a function in the typescript file, please the following code which uses angular material.

<mat-menu #menu3="matMenu" [overlapTrigger]="false">
   <button mat-menu-item [routerLink]="['A']">A</button>
   <button mat-menu-item [routerLink]="['C']">C</button>
   <button mat-menu-item [routerLink]="['D']">D</button>
   <button mat-menu-item [routerLink]="['B']">B</button>  
</mat-menu>
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You can add the menu items in an array, sort the array, then render it using *ngFor , smth similar to:

public menuItems: string[] = ["C", "B", "A"];
// use sort() to sort the array in ngOnInit() or wherever convenient
ngOnInit() {
   this.menuItems = this.menuItems.sort();
}

then in the template bind the soreted array to *ngFor

<mat-menu #menu3="matMenu" [overlapTrigger]="false">
   <button mat-menu-item
           *ngFor="let item of menuItems"
           [routerLink]="[item]">{{ item }} 
    </button> 
</mat-menu>

Check MDN for sort() docs Here for custom sorting, etc ...

about 4 years ago · Santiago Gelvez 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