Reputation: 11
in AngularJS when i want to know if my actually url starts with "/routing" for example, i use route-segment and i can call $routeSegment.startsWith("routing"):
$scope.routing = function() {
return $routeSegment.startsWith("routing");
};
and then i use ng-class in HTML
<li ng-class="{ 'active': routing() }"></li>
Now, i want to do the same in Angular 2, what can i use for do this?basically i want to know the url, and then with ngClass i can active the link of bootstrap
Thanks, I'm beginner with Angular 2
Upvotes: 0
Views: 1572
Reputation: 8523
You can use the location
service,
export class Foo {
constructor(private _location: Location) {}
routing () {
const pattern = /^(routing)/;
return pattern.test(this._location.path());
}
}
Here I'm using a regex to match location.path()
, but you can take another approach to matching the URL if you'd like.
Upvotes: 1