Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 - how to hide nav bar in some components

Tags:

html

angular

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> 
like image 271
User 123 Avatar asked Mar 30 '17 12:03

User 123


People also ask

How to hide nav bar angular?

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.

How do I hide the navbar on certain pages?

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."

What is navigation bar in angular?

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.


2 Answers

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(); } 
like image 190
Dan Avatar answered Sep 20 '22 17:09

Dan


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.

routing module

... const routes: Routes = [ { path: 'welcome', component: WelcomeComponent, data: { title: 'welcome', toolbar: false} }, ...]; 

toolbar.component

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;     }   } 

toolbar.html

<mat-toolbar color="primary" *ngIf="visible">   <mat-toolbar-row>     <span>{{viewedPage | titlecase}}</span>   </mat-toolbar-row> </mat-toolbar> 
like image 31
cbilliau Avatar answered Sep 21 '22 17:09

cbilliau