How to catch routes in * ngIf
So, I want the anchor element on my header to disappear when a certain page is hit. How can I catch the url in * ngIf when this page hit.
I have a title that will remain the same for all pages. Just need to hide the anchor element when I am directed to / home. How do I catch this "/ home" in * ngIf?
* ngIf = "href = '/ home'" doesn't work. Any alternatives?
+7
source to share
3 answers
//mycomponent.component.ts
class MyComponent {
constructor(private router: Router){
}
}
//mycomponent.component.html
<div *ngIf="router.url === '/some/route'">
</div>
+10
source to share
You can check the current route of the route using the method location.path()
and decide if the route will be activated /home
.
*ngIf="isHomeRouteActivated()"
code
//Inject `Location` dependency before using it
isHomeRouteActivated(): boolean{
//Find more better way to do it.
return this.location.path().indexOf('/home') > -1;
}
0
source to share