I need to hide certain components from my main home page like the navbar and footer when I'm logged in to my admin panel. My admin components are lazy-loaded based on an admin module when called. The necessary components are getting hidden as expected when on admin view if the routes are not dynamic i.e like /admin/login
, /admin/dashboard
etc. But the problem starts if the routes are dynamic like /admin/category/:categoryId
or /admin/user/:userId
and in these routes the necessary components like navbar and footer doesn't hide itself. I'm getting the dynamic ids for the routes using ActivatedRoute
in the necessary components. Below is the method I'm using on my main page to read the application routes and show/hide components accordingly.
Main.ts
import { Router, NavigationEnd } from '@angular/router';
public url: any;
constructor(private router: Router) {
this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
this.url = event.url;
}
})
}
Main.html
<div class="main__container">
<app-navbar
*ngIf="url !== '/admin' && url !== '/admin/dashboard' && url !== '/admin/post-article' && url !== '/admin/video' && url !== '/admin/login' && url !== '/admin/sign-up' && url !== '/admin/category/:categoryId'">
</app-navbar>
<app-footer
*ngIf="url !== '/admin' && url !== '/admin/dashboard' && url !== '/admin/category/:categoryId' && url !== '/admin/post-article' && url !== '/admin/video' && url !== '/admin/login' && url !== '/admin/sign-up'">
</app-footer>
</div>
What you need here is to define regular expressions and test against those. Or maybe it's enough for you to check the
string#includes(string)
function. I would also suggest to use a more reactive (rxjs like) approach.On my template I would have:
Where on the typescript file I would have:
You can read more about Regular Expressions on JavaScript here
Another approach of implementing the
shouldShowNavBar
would be using some array predicates likesome
: Like so:If you don't want to use the async keep your code as it was but do: