Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With routerLinkActive the root URL is always active. #9885

Closed
marian2js opened this issue Jul 7, 2016 · 14 comments
Closed

With routerLinkActive the root URL is always active. #9885

marian2js opened this issue Jul 7, 2016 · 14 comments

Comments

@marian2js
Copy link

I'm submitting a ...

[x] bug report
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
When routerLinkActive is used with the root URL, the active class is always added, since all the URLs are on the tree of the root URL.

Expected/desired behavior
I believe that routerLinkActive should only add the active class to the root URL when we are there, and not on any sub URL, otherwise it seems useless, since it will always evaluate as true.

Reproduction of the problem
Here is a Plunker with the issue: http://embed.plnkr.co/nHasN0pr4j2E5yUermiy/
Click on Test Tab and you will see both tabs active.

What is the expected behavior?
Only Test Tab should be active.

What is the motivation / use case for changing the behavior?
I have a set of tabs: Home, Section 1, Section 2, ..., Section N.
When you are in any section different from Home, I expect this tab not be active.

Please tell us about your environment:

  • Angular version: 2.0.0-rc.4
  • Browser: all
  • Language: all

I'm not sure if this is an issue or the expected behavior, but I believe it will be better to add an exception for the root URL, and only mark it as active when we are there.
Please let me know if you agree on this change, and I will submit a PR.

Thanks.

@brandonroberts
Copy link
Contributor

This is by design. The routerLinkActive directive marks a routerLink as active if contained in the UrlTree. Since the root is always contained, you need to use the [routerLinkActiveOptions]="{ exact: true }" which will only mark it as active if the current url is an exact match to the routerLink url.

@marian2js
Copy link
Author

@brandonroberts Many thanks, I didn't know about the exact option, it solves the issue perfectly.
I'll close this issue.

@crisz
Copy link

crisz commented Aug 10, 2017

Sorry for commenting a closed issue. Setting "exact: true" involves a problem: whether you have query params in your URL, it won't recognize that route as active.
So if you have a navbar and a URL with query params, the aforesaid class won't be applied to any of the items.
Is this the expected behavior?

@pptyasar
Copy link

@brandonroberts what about @crisz comment?

@brandonroberts
Copy link
Contributor

@pptyasar yes, this is expected behavior. Query params are optional, so they are not considered when checking the path for the active route.

@LorienOlive
Copy link

So, how can I include the queryParams when checking to see if the route is active?

@ryanovas
Copy link

Perhaps I'm missing a key part of the logic behind this design, but it seems to me that using query params shouldn't prevent a link from being marked as active. I'm also coming short on why it makes sense to have to specify that I don't want the homepage to be marked as active all the time. What is the use case for having 2 links marked as active that makes this the default behaviour?

@ricrps
Copy link

ricrps commented May 13, 2018

@LorienOlive , put [routerLinkActiveOptions]="{ exact: true }" only in the link to the root

@zelwyn
Copy link

zelwyn commented Aug 12, 2018

Thanks @brandonroberts works for me!

@LorienOlive
Copy link

LorienOlive commented Aug 17, 2018 via email

@darwinguz
Copy link

darwinguz commented Aug 26, 2018

thanks so much @brandonroberts this really works:
[routerLinkActiveOptions]="{ exact: true }"

@emilio-martinez
Copy link
Contributor

I know this issue is old and closed, but just faced this and I have to say: this is weird. Especially if you fin yourself only having to apply it to the root route 🤔

@roelvdboom
Copy link

I know this issue is quite old but I think still some people take a look at this issue. A solution would be to reroute your root to /index or '/home' and then the root won't always be active.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests