Collapsible Navbar Angular 4 Bootstrap 4

10,165

I have Used this in my Angular App link

Bootstrap Navbar

 <nav class="navbar navbar-toggleable-md navbar-light bg-faded  navbar-fixed-top">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false"
          aria-label="Toggle navigation" (click)="isActive = !isActive">
    <span class="navbar-toggler-icon"></span>
  </button>
   <a class="navbar-brand" href="#">Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown" [ngClass] = "{show : isActive}"> // ngClass used here
    <ul class="navbar-nav">
        <li class="nav-item dropdown [routerLinkActive]="['active']" appDropdown"> // directive used here
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

appDropdown Directive

import {Directive, HostListener, HostBinding} from '@angular/core';

  @Directive({
    selector: '[appDropdown]'
  })
  export class DropdownDirective {

    private isOpen:boolean = false;

    @HostBinding('class.open') get opened(){
      return this.isOpen;
    }
    constructor() { }

    @HostListener('click')open(){
      this.isOpen = true;
    }

    @HostListener('mouseleave')close(){
      this.isOpen = false;
    }
  }

You can also import third party JS to do this job but if we are using a JS framework why use another but still if you need it you can find it here it has a dedicated question for it link

Share:
10,165
Isaac Levin
Author by

Isaac Levin

.Net Developer by trade, but usually jump into random other environments as needed.

Updated on December 03, 2022

Comments

  • Isaac Levin
    Isaac Levin over 1 year

    Been struggling for a bit trying to get something working the exact way I want it. I have a Angular Universal App that has a navigation bar (vertical on top) and I want it to be responsive for mobile. I am targeting Bootstrap 4 Alpha 6 and sing ngx-bootstrap to wire it up. However no matter what I do, I cannot get all parts to work as expected.

    Here is my template file (no custom styles here)

    <div class="pos-f-t fixed-top">
         <nav class="navbar navbar-inverse navbar-toggleable-md">
            <div class="collapse navbar-collapse">
               <a class="navbar-brand" routerLink="">Blog</a>
               <ul class="navbar-nav ml-auto mt-2 mt-md-0">
                  <li class="nav-item hidden-lg-down">
                      <a routerLink="/blog" [routerLinkActive]="['router-link-active']" class="list-group-item">Blog</a>
                  </li> &nbsp;
                  <li class="nav-item">
                      <a routerLink="/about" [routerLinkActive]="['router-link-active']" class="list-group-item">About</a>
                  </li> &nbsp;
                  <li class="nav-item">
                      <a routerLink="/search" [routerLinkActive]="['router-link-active']" class="list-group-item">Search</a>
                  </li> &nbsp;
              </ul>
           </div>
         </nav>
    </div>
    

    I have tried a few SO posts as well as MISC walkthroughs in GitHub Issues, but I am not able at this time. On small viewfinders, I want there to be an icon that hides/shows the menu onclick (whether assigning a css class to a div or using something else). The issue that I am running into is that Since I am using Universal, I have no jQuery access, so I assume out of the box Bootstrap Nav will not work. Anyone who can help would be my hero.

  • Isaac Levin
    Isaac Levin almost 7 years
    Sorry to be that guy here, but I asked for help with a particular issue, and you linked me to basically design paradigms. If there are examples of doing what I want in there, I would appreciate direction here, at this point it is a lot to consume.
  • Dean Chalk
    Dean Chalk almost 7 years
    30 second look at the API docs (github.com/angular/flex-layout/wiki/API-Documentation) and I see this <div fxShow [fxShow.xs]="isVisibleOnMobile()"></div>
  • Steve In CO
    Steve In CO about 6 years
    While this does work, the cursor doesn't change to indicate it is a link when hovered. This could probably be fixed with some CSS, but other than that it does function.
  • regretoverflow
    regretoverflow over 5 years
    Nowhere does the OP say they using angular flex-layout, so you really didn't answer the op's question.