Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!