For an application built with AngularJs 2.0 (rc1) I wanted to do one simple thing: To highlight
the current active navigation bar item. However, I’ve had to do it with the new angular router,
the one that currently lacks documentation. So after a few searches online and digging through
the router object I’ve reached the solution I want to share.
Let’s start with the solution itself:
(Taken from the application’s entry component)
On the component’s constructor we subscribe to the router change, then, on the event handler, we send the _root member of the router’s routeTree member.
Each node value has its urlSegment array which we need to build our url string from, using Array.map method.
For nested routing we will recursively call our method with the node’s children.
There may will be more appropriate way to do this (going through ‘_root’ member doesn’t feel right)
but it works for now.