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?
To access route parameters and query parameters in Angular, use the ActivatedRoute service. The ActivatedRoute service provides Observables through which we can subscribe to the values of route params and route query params.
What is Wildcard Route in Angular? The Wildcard Route is basically used in Angular Application to handle the invalid URLs. Whenever the user enter some invalid URL or if you have deleted some existing URL from your application, then by default 404 page not found error page is displayed.
so if you try to use *ngIf to conditionally disable and enable router-outlet to overcome the problem it does not work. One router-outlet will get registered and no matter what you do, next router-outlet will not respond to the route changes.
mycomponent.component.ts
import { Router } from '@angular/router'
class MyComponent {
constructor(public router: Router) { }
}
mycomponent.component.html
<div *ngIf="router.url === '/some/route'">
<!-- … -->
</div>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With