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

242
Vistas
angular 2 active route is on single route

say that I have the following html that specifies links for different parts.

<a [routerLink]="['/']" [queryParams]="{'Analytics':700}" routerLinkActive="active" [routerLinkActiveOptions]="{exact:false}">Home</a>
      <input type="text" #id (input)="0"/>
      <a [routerLink]="['user',id.value]" [queryParams]="{'Analytics':500}" routerLinkActive="active" [routerLinkActiveOptions]="{exact:false}">User</a>

      <button type="button" class="btn btn-default" (click)="onNavigate()">click me</button>

      <hr>

what I expect is that for a given path like /user/15/edit my both links became red (are affected by active class)however they work for just exact paths that is just when I click on the very link ,that would be activated.

enter image description here

enter image description here

the above url is that the User link refers to.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use ngClass in this case to add an active class based on some routing logic. This is in some shape or form what routerLinkActive does. So you can do something like this:

<a [routerLink]="['/']" [queryParams]="{'Analytics':700}" [ngClass]="{ 'active' : isMyRoute }">Home</a>

And in your component use Router and ActivatedRoute to check for the component name by traversing the route tree that is displayed in router outlet and toggle a variable based on that:

import {ActivatedRoute, NavigationEnd, Router} from '@angular/router';
... 
public isMyRoute: boolean;

constructor(private router: Router, private route: ActivatedRoute) {}

ngOnInit() {
    this.router.events.subscribe(res => {
      if (res instanceof NavigationEnd) {
        let fn = this.route.children[0].component;
        let componentName = fn['name'];

        if (componentName === 'UserEditComponent') {
          this.isMyRoute = true;
        } else {
          this.isMyRoute = false;
        }
      }
    })
}
over 4 years ago · Santiago Trujillo 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