1

In my Angular4 app I created a standard nav bar. When the screen size decreases the nav menu dropdown appears. When clicking on the menu dropdown, the (collapsed) links are not shown.

Question: how can I get the menu dropdown working for Angular 4 + Bootstrap 3?

I created a standard component. This is the template file:

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a routerLink="/search" class="navbar-brand">Brand-name</a>
    </div>

    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li><a routerLink="/search">Search</a></li>
        <li><a routerLink="/edit">Edit</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a routerLink="/signup">Signup</a></li>
      </ul>
    </div>
  </div>
</nav>

Of course I read similar questions and articles, but none helped me any further. Most are about Boostrap only, not the combination with Angular 4.

2
  • Bootstrap comes with some jquery libraries for collapsing stuff. Did you import them? getbootstrap.com/docs/4.0/getting-started/introduction/… Commented Sep 22, 2017 at 18:42
  • Yes, I inserted jquery.3.2..0.js and bootstrap.js to index.html. Is that the right place? I did not place the header-html as part of the index.html because the routerlinks wouldn't work. Should I import them via the scripts tag of angular-cli? Please show what I should include where. Workig with a seperate <header> seems to be a good way. Commented Sep 22, 2017 at 18:53

3 Answers 3

1

The Bootstrap Drop down adds a class open to the <ul> element for the drop down to appear and close , you can make use of Angular Directives to add the Same behaviour to the Component.

App Drop Down 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;
    }

Stick it to your ul <ul class="nav navbar-nav" appDropdown>

A working Example Code

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. When clicking on the hamburger the menu opens. OK. ISSUE - on clicking on a menu item, how to close it again?
@tjm1706 my code has a eveny on mouse leave. You need to add a different event for that. That itself is a new question as you need to check where the click came from
Sighn - Clicking on the menu item is the real issue. I tried many different approaches. Please HELP. This is of course only applicable on small screens when there is a dropdown menu due to the size of the screen.
@tjm1706 you need to another directive for the same i guess, if you want to acheive it in angular way
How? Can you show me a solution? I guess this is a very general question for angular developers.
|
1

All answers so far show how to open the dropdown menu. The answers don't show how to collapse that dropdown menu again after selecting on a menu item.

Thanks to Thomas Rundle the basic parts can be found in the following solution. I added some code. The header.component.ts is:

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
  isIn : boolean = false;   // store state
  constructor() { }
  ngOnInit() {
  }
  toggleState() { // click handler
    let bool = this.isIn;
    this.isIn = bool === false ? true : false;
  }
}

The template header.component.html is:

<nav class="navbar navbar-default">
  <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> <!-- #1 -->
      <a class="navbar-brand" routerLink="/page1">Project Manager</a>
    </div>
    <div class="collapse navbar-collapse in" [ngClass]="{ 'in': isIn }">
      <ul class="nav navbar-nav">
        <li><a role="tab" (click)="toggleState()" data-toggle="tab" routerLink="/page1">Page 1</a></li>
        <li><a role="tab" (click)="toggleState()" data-toggle="tab" routerLink="/page2">Page 2</a></li>
      </ul>
    </div> <!-- #2 -->
  </div>
</nav>

Comments

0

https://plnkr.co/edit/oSKnNWXNafbMjCiPwqrv?p=preview

since you have target="#navbar", you have to set the id of the target to navbar for it to work.

    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a routerLink="/search" class="navbar-brand">Brand-name</a>
            </div>

            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li><a routerLink="/search">Search</a></li>
                    <li><a routerLink="/edit">Edit</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li><a routerLink="/signup">Signup</a></li>
                </ul>
            </div>
        </div>
    </nav>

1 Comment

Thank you. When I try the responsiveness, the dropdown does not disappear on medium to larger screens. You can see that behaviour without your code. Because it is a failry common code, can you help a bit further?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.