Angular 8 and Bootstrap 4 Integration: Navbar Functionality Working, but Issue with Auto-Closing on Click Action (Both Inside and Outside Navbar)

While using ng-bootstrap with Angular 8, I encountered a problem with the navbar. The navbar functions properly by being responsive and opening/closing when clicking the hamburger icon. However, the issue arises when it does not automatically close when a link is clicked or when the user clicks outside of the navbar. It only closes if the user clicks on the hamburger icon again. Is there a way to configure ng-bootstrap to make the navbar autoclose upon click?

 isCollapsed = false;
 <nav class="navbar navbar-expand-lg navbar-light">
    <button
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarNavDropdown"
      aria-controls="navbarNavDropdown"
      aria-expanded="false"
      aria-label="Toggle navigation"
      (click)="isCollapsed = !isCollapsed"
      [attr.aria-expanded]="!isCollapsed"
    >
      <span class="navbar-toggler-icon"></span>
    </button>

    <!--Logo-->
    <div class="sidebar-header d-none d-lg-block">
      <img src="assets/img/logo_color_cloudev.png" alt="cloudev-logo" />
    </div>

    <div class="collapse navbar-collapse" id="navbarNavDropdown" [ngbCollapse]="isCollapsed">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" routerLink="/dashboard" routerLinkActive="active" (click)="isHide()"><i class="fa fa-desktop dash"></i><span>Dashboard</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" routerLink="/roles" routerLinkActive="active" *ngIf="this.isAdmin"><i class="fa fa-gg-circle icon"></i>Roles</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" routerLink="/users" routerLinkActive="active" *ngIf="this.isAdmin"><i class="fa fa-users user"></i>Users</a>
        </li>
        <!--Dropdown-->
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fa fa-line-chart leaves"></i><span>Leave Management</span>
          </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <a class="dropdown-item" routerLink="/user-leaves" *ngIf="!this.isAdmin" routerLinkActive="active">My leaves</a>
            <a class="dropdown-item" routerLinkActive="active" *ngIf="this.isAdmin" routerLink="/leave-types">Leave types</a>
            <a class="dropdown-item" routerLinkActive="active" *ngIf="this.isAdmin" routerLink="/leave-request-admin">Leave Requests</a>
            <a class="dropdown-item" routerLink="/holidays" routerLinkActive="active">Holidays</a>
          </div>
        </li>
        <!--Toggle for header-->
        <div class="fullview">
          <hr class="d-block d-lg-none" />
          <li class="nav-item">
            <a class="nav-link d-block d-lg-none" routerLink="/my-profile" routerLinkActive="active"><i class="fa fa-user-md icon"></i>My profile</a>
          </li>
          <li class="nav-item">
            <a class="nav-link d-block d-lg-none" routerLink="/passwordchange" routerLinkActive="active"><i class="fa fa-key icon"></i>Reset password</a>
          </li>
          <li class="nav-item">
            <a class="nav-link d-block d-lg-none" (click)="logout()" routerLinkActive="active"><i class="fa fa-sign-out icon"></i>Logout</a>
          </li>
        </div>
      </ul>
    </div>
  </nav>

Answer №1

Here are the attributes you can utilize to collapse the designated element:

data-toggle="collapse" data-target="#navbarNavDropdown"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Fetching data from an Angular Universal server API

Recently, I updated my application to Angular 6 + Universal and it has been a positive experience overall. However, there are two specific issues that I am currently facing. I would greatly appreciate assistance in resolving both problems: 1- Is there a w ...

Issue: The Angular build encountered an error stating that the 'AggregateQuerySnapshot<T>' generic type requires one type argument after running npm install

Recently, I encountered a new error in my Angular project after performing an npm upgrade for maintenance purposes. Strangely, the upgrade did not involve changing any direct dependency versions. The problem arises when trying to run 'ng serve,' ...

Error in Angular 4: Undefined property 'replace' causing trouble

I've been trying to use the .replace() JavaScript function in Angular 4 to remove certain characters from a string. Here is the code snippet from my component: @Component({...}) export class SomeComponent implements OnInit { routerUrl: string = &apo ...

What is the reason for instances being compatible even if their class constructors do not match?

Why are the constructors in the example below not compatible, but their instances are? class Individual { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } class Worker { name: st ...

"Silently update the value of an Rxjs Observable without triggering notifications to subscribers

I'm currently working on updating an observable without alerting the subscribers to the next value change. In my project, I am utilizing Angular Reactive Forms and subscribing to the form control's value changes Observable in the following manner ...

Nested formArrays within formArrays in Angular 4

I've been working on implementing a FormArray inside another FormArray, but it doesn't seem to be functioning correctly. I also tried the solution provided in the link below, but it didn't work for me. How to get FormArrayName when the Form ...

React "Node not found on a component that has been unmounted"

Attempting to combine Bootstrap with React has been a bit challenging for me. I've been working on utilizing the Collapse class from reactstrap to create a responsive Navbar, but I keep getting an error message from React: react-dom.development.js:55 ...

Dealing with the "net::ERR_CERT_DATE_INVALID" issue within Ionic 4

When sending an "http" request in Ionic4, the response neither falls under the success callback nor gets handled by the error handling block. It just keeps loading indefinitely. I need a solution to properly handle this error in Ionic4 (Client side). I at ...

The issue arises when attempting to apply CSS styles to an existing component or body tag,

I am currently working on an Angular 7 project where I have a requirement to dynamically load a component using routes. However, when I try to add styles for the body tag and existing component tags in the dynamically loaded component style-sheet, they do ...

The module located at "c:/Users//Desktop/iooioi/src/main/webapp/node_modules/rxjs/Rx" does not have a default export available

I am currently delving into the realm of RxJs. Even after installing rxjs in package.json, why am I still encountering an error that says [ts] Module '"c:/Users//Desktop/iooioi/src/main/webapp/node_modules/rxjs/Rx"' has no default export ...

Using TypeScript and Angular to modify CSS properties

I'm trying to figure out how to change the z-index CSS attribute of the <footer> element when the <select> is open in TypeScript (Angular 10). The current z-index value for the footer is set to 9998;, but I want it to be 0;. This adjustmen ...

Expand the size of the imported gltf model within Three.js

After successfully loading a 3d model with a gltf extension using the GLTFLoader in Three.js, I encountered a new challenge. I needed to adjust the dimensions of the model dynamically when the window is resized, based on the values of window.innerWidth and ...

Ensuring type signatures are maintained when wrapping Vue computed properties and methods within the Vue.extend constructor

Currently, I am trying to encapsulate all of my defined methods and computed properties within a function that tracks their execution time. I aim to keep the IntelliSense predictions intact, which are based on the type signature of Vue.extend({... Howeve ...

"Utilizing the Ionic side menu to activate a function in the view.ts file

In my app, there is a menu located in the main app.component.html <ion-app> <ion-split-pane contentId="main-content"> <ion-menu contentId="main-content" type="overlay"> <ion-content> ...

Instructions on opening a modal and changing the source of the iframe within the modal

I currently have a modal within my application that is triggered by Bootstrap: <div class="modal full-page fade" tabindex="-1" role="dialog" id="fullpageModal"> <div class="full-page-content"> <button type="button" class="close" d ...

Can TypeScript be implemented within nuxt serverMiddleware?

I recently began diving into the world of nuxtjs. When setting up, I opted to use typescript. Initially, everything was running smoothly until I decided to incorporate express in the serverMiddleware. Utilizing the require statement to import express funct ...

Deactivate input fields in FormArray using a checkbox

I am currently working on a multi-step form that includes a FormArray for schools in the third step. Within this FormArray, there is an input field called 'endDate' where users can check a checkbox labeled 'En Curso' (in progress) if th ...

Steps to converting an enum or literal

Hey there, I'm relatively new to working with TypeScript and I've been experimenting with transforming enums/literals using functions. For instance, creating a capitalize function that capitalizes the first letter of a string. (e.g., mapping typ ...

RobotFramework | Select File | Angular | Not functioning

I'm having trouble understanding how to utilize the "Choose File" feature. My intention is to upload the file C://RobotAutomation/Customers/in/test.csv on the following website This is what the website looks like: https://i.stack.imgur.com/HZbhA.pn ...

Utilizing multiple parameters as variables in Angular's router

Here is an example of how my router configuration will be set up: { path: ':firstVariable', component: FirstComponent }, { path: ':secondVariable', component: Secon ...