Skurt
Skurt

Reputation: 11

Angular 2 I need to know when my url starts with

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

Answers (1)

Jack Guy
Jack Guy

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

Related Questions