15

So I want to make an anchor element on my Header disappear when a specific page is hit. How can I catch the url in the *ngIf when that page is hit.

I have a header which will remain same for all pages. Just need to hide an anchor element when I am routed at /home. How to catch this "/home" in *ngIf?

*ngIf = "href='/home'" is not working. Any alternatives?

David Wolf
  • 1,400
  • 1
  • 9
  • 18
Ravy
  • 3,599
  • 3
  • 11
  • 14

3 Answers3

26

mycomponent.component.ts

import { Router } from '@angular/router'

class MyComponent {
    constructor(public router: Router) { }
}

mycomponent.component.html

<div *ngIf="router.url === '/some/route'">
    <!-- … -->
</div>
David Wolf
  • 1,400
  • 1
  • 9
  • 18
Always Learn
  • 662
  • 6
  • 14
9

I came to this page because I had the same issue than Ravy but the proposed solutions were not ok for me.

The accepted answer is not working if you are using route path with query params.

Here's the solution I use:

mycomponent.component.ts

import { Router } from '@angular/router'

class MyComponent {
    constructor(public router: Router) { }
}

mycomponent.component.html

<div *ngIf="router.isActive('/some/route')">
    <!-- … -->
</div>
David Wolf
  • 1,400
  • 1
  • 9
  • 18
Jerome2606
  • 933
  • 20
  • 42
-3

// mycomponent.component.ts

class MyComponent {
    constructor(public router: Router){

    }
}

// mycomponent.component.html

<div *ngIf="this.router.url === '/some/route'">

</div>