I am created nav bar separately in nav.component.html ,how to hide nav bar in some components like login.component.
nav.component.html
<nav class="navbar navbar-default navbar-fixed-top navClass"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" (click)="toggleState()"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <div class="collapse navbar-collapse" [ngClass]="{ 'in': isIn }"> enter code here <ul class="nav navbar-nav"> <li class="active"><a href="#">Home</a></li> <li><a href="#">about</a></li> </ul> </div> </div> </nav>
First for it will check for showNav variable but it will not be available , so it will return false for the other pages where we want to show menu , so need to declare that variable any other pages. In login page we set the value to true, so it will make it false and hide the nav.
Go to your Navbar settings and find the navigation item you want to hide for a particular page. Click to edit and assign it a classname. You could assign it something like "hide-navigation-item."
The Navbar in Angular Navbar allows users to easily find what they are looking for on your web applications. There are many methods for navigation to achieve simple to complex routing. Angular provides a separate module that helps us create a navbar in our web application and use it for navigation.
Navbar control and formatting is often needed throughout an app, so a NavbarService is useful. Inject in those components where you need.
navbar.service.ts:
import { Injectable } from '@angular/core'; @Injectable() export class NavbarService { visible: boolean; constructor() { this.visible = false; } hide() { this.visible = false; } show() { this.visible = true; } toggle() { this.visible = !this.visible; } doSomethingElseUseful() { } ... }
navbar.component.ts:
import { Component } from '@angular/core'; import { NavbarService } from './navbar.service'; @Component({ moduleId: module.id, selector: 'sd-navbar', templateUrl: 'navbar.component.html' }) export class NavbarComponent { constructor( public nav: NavbarService ) {} }
navbar.component.html:
<nav *ngIf="nav.visible"> ... </nav>
example.component.ts:
import { Component, OnInit } from '@angular/core'; import { NavbarService } from './navbar.service'; @Component({ }) export class ExampleComponent implements OnInit { constructor( public nav: NavbarService ) {} } ngOnInit() { this.nav.show(); this.nav.doSomethingElseUseful(); }
I was able to solve this without using a nav/toolbar service by adding a data object to the route in the route.module. I expanded on Todd Motto's example of adding dynamic titles to a page and added toolbar: false/true
to the data object in my path. I then subscribed to the router events in my toolbar.component. Using Todd's event listener func, I read the path object and used the boolean value to set the toolbar visible or not visible.
No service needed and works on pagerefresh.
... const routes: Routes = [ { path: 'welcome', component: WelcomeComponent, data: { title: 'welcome', toolbar: false} }, ...];
constructor(private router: Router, private activatedRoute: ActivatedRoute, public incallSvc: IncallService) { this.visible = false; // set toolbar visible to false } ngOnInit() { this.router.events .pipe( filter(event => event instanceof NavigationEnd), map(() => this.activatedRoute), map(route => { while (route.firstChild) { route = route.firstChild; } return route; }), ) .pipe( filter(route => route.outlet === 'primary'), mergeMap(route => route.data), ) .subscribe(event => { this.viewedPage = event.title; // title of page this.showToolbar(event.toolbar); // show the toolbar? }); } showToolbar(event) { if (event === false) { this.visible = false; } else if (event === true) { this.visible = true; } else { this.visible = this.visible; } }
<mat-toolbar color="primary" *ngIf="visible"> <mat-toolbar-row> <span>{{viewedPage | titlecase}}</span> </mat-toolbar-row> </mat-toolbar>
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