简体   繁体   English

将菜单状态更改为在 angular 8 中处于活动状态

[英]change menu status to active in angular 8

I am new in angular I am trying to change the state of my button in the navbar, taking into consideration the navigation route, however when clicking my button I select all the other buttons including the button pressed.我是 angular 新手,考虑到导航路线,我正在尝试更改导航栏中按钮的状态,但是当单击我的按钮时,我选择了所有其他按钮,包括按下的按钮。

Here I have my method which I call on the button:在这里,我有我在按钮上调用的方法:

 private status = false; selectOnMenu(event) { const urlBase = this.location.path(); if (urlBase === '/documents/index/my') { this.status = !this.status; } else if (urlBase === '/documents/index/sending') { this.status = !this.status; } else if (urlBase === '/documents/index/receiving') { this.status = !this.status; } else if (urlBase === '/documents/index/received') { this.status = !this.status; } else if (urlBase === '/documents/index/finished') { this.status = !this.status; } else { this.status = status; } }

and this is the menu of my buttons:这是我的按钮菜单:

 <div class="btn-group" role="group" aria-label="..."> <a class=" btn btn-square" (click)="selectOnMenu($event)" [ngClass]="status ? 'active' : ''" routerLink="/documents/index/my">MIS DOCUMENTOS</a> <a class=" btn btn-square" (click)="selectOnMenu($event)" [ngClass]="status ? 'active' : ''" routerLink="/documents/index/sending">PENDIENTES POR DESPACHAR</a> <a class=" btn btn-square" (click)="selectOnMenu($event)" [ngClass]="status ? 'active' : ''" routerLink="/documents/index/receiving">PENDIENTES POR RECEPCIONAR</a> <a class=" btn btn-square" (click)="selectOnMenu($event)" [ngClass]="status ? 'active' : ''" routerLink="/documents/index/received">RECEPCIONADOS</a> <a class=" btn btn-square" (click)="selectOnMenu($event)" [ngClass]="status ? 'active' : ''" routerLink="/documents/index/finished">FINALIZADOS</a> </div>

I have tried to change my method but it still marks all the buttons when I click, which is incorrect, since only the selected button should be highlighted.我试图改变我的方法,但当我点击时它仍然标记所有按钮,这是不正确的,因为只应突出显示选定的按钮。

在此处输入图片说明

Your error is using same variable (status) for all buttons.您的错误是对所有按钮使用相同的变量(状态)。 And ofcourse ngClass will behave same for all.当然 ngClass 对所有人的行为都是一样的。 If you want to use ngClass you must use seperate variables for each control or for this feature use routerLinkActive="active" property.如果您想使用 ngClass,您必须为每个控件使用单独的变量,或者为此功能使用 routerLinkActive="active" 属性。

You are doing too much as a new bee.作为一只新蜜蜂,你做得太多了。

use routerLinkActive , once that route will be active that active class will automatically be selected, explore the link使用routerLinkActive ,一旦该路由处于活动状态,将自动选择活动类,探索链接

for example:例如:

<a class=" btn btn-square" 
 routerLinkActive="active"  
 routerLink="/documents/index/my">MIS DOCUMENTOS</a>

You can look at RouterLinkActive in Angular which will do the job.您可以在 Angular 中查看RouterLinkActive来完成这项工作。

<a routerLink="/documents/index/sending" routerLinkActive="active">Bob</a>

And you won't even need the call to the selectOnMenu()你甚至不需要调用selectOnMenu()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM