Friday, August 4, 2017

How do I check what the current route is in an Angular 4 app to conditionally hide and show stuff at an ever-present header or sidebar component?

At your component you will have to have this import:

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

 
 

Loop the import in at the constructor's signature like so:

public whereAmi: ActivatedRoute

 
 

Use it like this:

isToDisplayCompanyDropdown():boolean{
   if(this.whereAmi.snapshot.firstChild.url[0].path == 'generic-gunk'){
      return false;
   }
   return true;
}

 
 

As you might imagine you could use isToDisplayCompanyDropdown() in the template in an *ngIf inline in an ng-container wrapping a dropdown list of companies.

No comments:

Post a Comment