2

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
  • 2,809
  • 9
  • 49
  • 88
  • 1
    helpfull posts https://stackoverflow.com/q/49010282/8632727 and https://stackoverflow.com/q/48058827/8632727 – Patata Mar 31 '18 at 08:11

5 Answers5

2

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

Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
0

Just add in your ts file:

export class HeaderComponent implements OnInit {
    public isCollapsed = true;
    constructor() {
    }
    ngOnInit() {
    }
}

Then In your component.html add (click)="isCollapsed = !isCollapsed" in button

<div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar" (click)="isCollapsed = !isCollapsed"
        [attr.aria-expanded]="!isCollapsed">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
    </button>
    <a class="navbar-brand" href="#"><img class="navbar-logo" src="../../assets/images/3.png"></a>
</div>

Then in your div where you have to show collapse item add [ngbCollapse]="isCollapsed"

<div class="collapse navbar-collapse" id="myNavbar" [ngbCollapse]="isCollapsed">
Jay Momaya
  • 1,831
  • 19
  • 32
0
#you can use click method in your html and use router navigate in your type script.

#In HTML File
     <a class="nav-link" (click)="onMenuClick('profile')">Profile </a>
#In Type Script file

 onMenuClick(path) {
    $('.navbar-toggler').click();
    this.router.navigate(['/' + path]);

  }
  • 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. – Steve In CO May 06 '18 at 02:03
0

Use this site for Angular compatible collapsable navbar. It's very easy.

Collapsable Navbar

RichIntellect
  • 191
  • 2
  • 4
-4

You may find the answer to your issues with this

https://github.com/angular/flex-layout

It gives you a broad layout API for Flex and Media Query manipulation

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • 2
    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. – Isaac Levin Jul 24 '17 at 15:43
  • 30 second look at the API docs (https://github.com/angular/flex-layout/wiki/API-Documentation) and I see this
    – Dean Chalk Jul 24 '17 at 15:46
  • 1
    Nowhere does the OP say they using angular flex-layout, so you really didn't answer the op's question. – regretoverflow Nov 17 '18 at 17:50