1

I'm trying to make something like here: AngularJS show div based on url/condition

<span id="page-locator" *ngIf="location.path() == '/overview'">test</span>

but it doesn't work.

How can I achieve this in Angular2?

Thanks.

Community
  • 1
  • 1
eric.dummy
  • 399
  • 1
  • 8
  • 24
  • 1
    if you are looking for current page then check this link: H[ow do I get the absolute path of the current page in Angular 2?](http://stackoverflow.com/questions/37517183/how-do-i-get-the-absolute-path-of-the-current-page-in-angular-2) – sameer Apr 10 '17 at 13:26
  • I'm trying to make if work with ngIf.. but thanks. – eric.dummy Apr 10 '17 at 13:35

3 Answers3

3

It looks like you're trying to use the path of the Angular router, correct? If so, you can inject the Router service into your component and use that in your view:

import { Router } from '@angular/router';

export class SomeComponent {
    constructor(public router: Router) { }
}

Then in your component template you can access the current router url via:

<span id="page-locator" *ngIf="router.url === '/overview'">test</span>
DRiFTy
  • 11,269
  • 11
  • 61
  • 77
0

window.location is where you can get your information!

for an absolute path you can go this way :

constructor(location:Location) {
  console.log(location.prepareExternalUrl(location.path()));
}
Felix Haeberle
  • 1,530
  • 12
  • 19
0

Try the following solution :

location: any;
constructor(private _router: Router) { 
      _router.events.subscribe((data:any) => { this.location = data.url; });
}

The data is an object and we need the url property contained in that object.

<span id="page-locator" *ngIf="location == '/overview'">test</span>
bougsid
  • 94
  • 1
  • 4