🔗 How to Highlight a Link When It Has Multiple Child Links in Angular
When building a sidebar in Angular, you might want to highlight a parent link when any of its child routes are active. Angular’s routerLinkActive
only works for single routes — so here’s a quick 2-step solution using a custom directive.
-
Step 1: Create a custom directive
Write a directive that checks the current URL against a list of child links and adds anactive
class if any match.
Step 1: Directive Code
-
Step 2: Use it in your template
On your parent menu link, pass the list of child URLs to the directive.
Step 2: Template Code
✅ Works with multiple child links, query parameters, and is easy to reuse.
✅ This blog is written by TechJourney with Gagan, sharing practical Angular tips for developers.
No comments:
Post a Comment