Angular4 does not correctly collapse the Bootstrap 4 Navbar

Currently, I'm utilizing Bootstrap 4 and Ng-Bootstrap within an Angular 4 Project.

I've included the bootstrap.css in my angular-cli.json as follows:

"styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.css",
    "styles.css"
],

In addition, I have imported the NgbModule in my app.module structured like this:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { AppComponent } from './app.component';

@NgModule({
 declarations: [
   AppComponent
 ],
 imports: [
   BrowserModule,
   NgbModule.forRoot()
 ],
 providers: [],
 bootstrap: [AppComponent]
})
export class AppModule { }

I've constructed a Navbar by emulating the instructions from the Bootstrap website and integrated the toggle component from ng-bootstrap (which is functional). The code snippet appears below:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
 <a class="navbar-brand" href="#">Navbar</a>
 <button class="navbar-toggler" type="button" data-toggle="collapse" data-
 target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-
 expanded="false" aria-label="Toggle navigation">
 <span class="navbar-toggler-icon"></span>
 </button>

 <div class="collapse navbar-collapse" id="navbarSupportedContent">
   <ul class="navbar-nav mr-auto">
     <li class="nav-item active">
       <a class="nav-link" href="#">Home <span class="sr-only">(current)</span>
 </a>
     </li>
     <li class="nav-item">
       <a class="nav-link" href="#">Link</a>
     </li>
     <li>
       <div ngbDropdown class="d-inline-block">
         <div class="nav-link" id="dropdownBasic1" ngbDropdownToggle>Toggle 
 dropdown</div>
         <div ngbDropdownMenu aria-labelledby="dropdownBasic1">
           <button class="dropdown-item">Action - 1</button>
           <button class="dropdown-item">Another Action</button>
           <button class="dropdown-item">Something else is here</button>
         </div>
       </div>
     </li>
     <li class="nav-item">
       <a class="nav-link disabled" href="#">Disabled</a>
     </li>
   </ul>
   <form class="form-inline my-2 my-lg-0">
     <input class="form-control mr-sm-2" type="search" placeholder="Search" 
 aria-label="Search">
     <button class="btn btn-outline-success my-2 my-sm-0" 
 type="submit">Search</button>
   </form>
 </div>
</nav>

The issue arises when I resize the window and trigger the collapse function of the navbar; it doesn't respond as expected.

I attempted to install and link the JavaScript files for jquery, popper, and bootstrap, but unfortunately, it still isn't functioning correctly. Below is how the current angular-cli.json file appears:

"styles": [
   "../node_modules/bootstrap/dist/css/bootstrap.css",
   "styles.css"
 ],
 "scripts": [
   "../node_modules/popper.js/dist/umd/popper.min.js",
   "../node_modules/jquery/jquery.min.js",
   "../node_modules/bootstrap/dist/js/bootstrap.min.js"

 ],

What could be causing this problem?

Answer №1

When using Angular, make sure to implement this solution

Start by declaring the following in your class public navbarCollapsed = true;

then modify your button like below

<button class="navbar-toggler" type="button" (click)="navbarCollapsed = !navbarCollapsed" [attr.aria-expanded]="!navbarCollapsed" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
</button>

Additionally, add the following code to your collapsing div

<div class="collapse navbar-collapse" [ngbCollapse]="navbarCollapsed" id="navbarSupportedContent"> 

By following these steps, you can eliminate the need for jquery, popper, or bootstrap.js.

For more information on this issue, refer to this link.

You can also view ng-bootstrap's demo page code here.

Answer №2

I encountered a similar issue and decided to utilize regular Bootstrap 4 instead of ng-bootstrap, eliminating the need for any additional modules in the app.module file. It worked like a charm. Below is the snippet to include in angular.json (Angular v6) for Bootstrap 4+:

"scripts": ["../node_modules/jquery/dist/jquery.js",
            "../node_modules/popper.js/dist/umd/popper.js", 
             "../node_modules/bootstrap/dist/js/bootstrap.js"]

As for the CSS, I opt for the Scss version, which can be configured as follows.

"styles": [
              "src/styles.scss",
              "../node_modules/font-awesome/scss/font-awesome.scss"
            ],

Additionally, don't forget to import Bootstrap into the style.scss file.

@import "~bootstrap/scss/bootstrap.scss";

Cheers! JK

Answer №3

Make sure to check the compiled classes to ensure you have the correct version. I once encountered a problem with the class col-sm-offset-#, only to realize it didn't exist in that particular version. It's possible you're facing a similar issue.

You can find the files at this location: ..\node_modules\bootstrap\dist\js, which contains bootstrap.js and bootstrap.min.js.

If you can't locate the nav classes in bootstrap.js, consider looking for an alternative Bootstrap repository.

Best of luck!

Answer №4

Everything is running smoothly for me I went ahead and set up everything by executing

npm i  --save @ng-bootstrap/ng-bootstrap ng add @angular/localize

  • Remember to update app.module.ts file accordingly

import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

imports:[NgbModule]

Strange that NgbModule.forRoot() isn't functioning as expected

  • In your angular.json file, ensure you have the necessary CSS and JS references added:
    "styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css" ], 
    "scripts": ["node_modules/jquery/dist/jquery.min.js",
    "node_modules/bootstrap/dist/js/bootstrap.js]`

Hopefully this information sheds some light on your issue

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

Visuals Divide the Text

"Incorporate visual elements to enhance the content." Can anyone explain the concept? How can it be implemented using html or css? Much appreciated. ...

What could be causing my tabs (such as HOME, ABOUT ME..) not displaying the correct paragraph or section content?

I have set up navigation tabs on my website using anchor tags, but they are currently not linked to any specific paragraphs. I want the corresponding paragraph to be displayed when a tab is clicked, but I'm unsure how to add this functionality using j ...

Problem with the hover functionality of the <li> tag

I'm having an issue with applying the hover property to the li element in the sidebar. The hover effect works, but the icon tag is not showing the hover effect. I want the icon to also show the hover effect along with the li's hover effect. Here ...

Tips for importing bootstrap scss efficiently in an angular project to prevent style duplication

Is there a way to optimize an Angular project for utilizing Bootstrap's mixins and variables without constantly importing the styles in every component? Currently, the project I'm working on imports the variables.scss file multiple times due to u ...

Bootstrap 4: Inline Button with Full Width Styling

Although the title may seem misleading, I struggled to come up with a better one. What I need is to have an image aligned with text within a div, and a button below that text. However, I'm facing difficulty in making the button take up the full width ...

Tips for expanding full screen slider dimensions

I am currently working on a website project using ASP.NET MVC Core 1.1.2. I want to create a full-screen slider for the homepage that appears stretched both on desktop and mobile browsers. However, when I view the slider on my browser, there is an annoying ...

Align list item span to the center

I have encountered an issue with creating a match list using li and ul tags. Despite applying the center span to a fixed width and the team spans to the remaining space, the widths still appear different. How can I ensure that the team spans have the same ...

Broadcast the latest N occurrences

I am working on an Angular 6 application where I want to display the most recent N events from a continuous stream of events coming from a web-socket. Currently, the data is shown using RxJS Observable<Event[]>: <div *ngFor="let event of (wsEven ...

React is not applying the font as expected

Currently, I am working on setting up a @font-face in my index.css file: @font-face { font-family: "Raleway"; src: url(../assets/fonts/Raleway/Raleway-VariableFont_wght.ttf) format(truetype), } Next, I am trying to apply this font family ...

Angular making sequential api calls

I have the following code where I am making one API call after another, with the second API call nested inside a subscribe. Is it possible to use mergeMap in this scenario to avoid multiple subscribe calls? Here is my current code: saveEmployees(empObje ...

Expanding a table cell to cover multiple columns in CSS when neighboring cells are vacant

I am new to CSS and facing an issue with a dynamically generated HTML table. Only one cell contains data, while the adjacent cells remain empty. <tr> <td> Really long data that is forced to wrap around.</td> <td> empty cell</td& ...

Issues with Angular 4 Animations not functioning properly have been reported on iOS Safari versions 10 and 11

Lately, I've been hearing a lot of buzz about Angular animations in this community. However, none of the solutions seem to be working for me. Currently, I am utilizing Angular 4 with angular-cli v1.1.1 and have incorporated Angular animations in vario ...

Is there a way to properly direct to an internal page while utilizing [routerLink] and setting target="_blank" without triggering a full page reload?

I'm attempting to use [routerLink] along with target="_blank" to direct to an internal page without triggering a full app reload. Currently, the page opens in a new tab, but the whole application is refreshed. Here is the HTML: <a [routerLink]=" ...

Exploring the concept of Server-Side Rendering in AngularIO

Exploring the fundamentals of SSR and AngularIO (latest version) has been quite enlightening. The distinction between CSR and SSR seems obvious based on their names. CSR carries out all the logic in the client side - for example, <app-route> remains ...

A guide on fixing the issue of the missing X-Frame-Options header in Angular

I'm currently working on an Angular-14 project. One challenge I'm facing is: The X-Frame options are missing - The X-Frame-Options header is either not set or set improperly to DENY or SAMEORIGIN. This can leave the application vulnerable to c ...

'view' is not recognized as a valid property of the 'div' element in Ionic3, so it cannot be bound

I am a beginner with Ionic and I'm trying to incorporate Angular Calendar into my Ionic3 application. However, I am encountering an error that says, "Can't bind to 'view' since it isn't a known property of 'div'. Here&ap ...

Equal gutters are present between CSS floats

After conducting various experiments, I have devised a method to incorporate fixed and equal gutters between floats. My approach involves using positive and negative margins as well as multiple wrappers, making the solution somewhat cumbersome. However, I ...

What is preventing us from setting a child's width and height to match that of the parent element?

Parent element is a div with a width of 300px and a height of 40px, containing a child input. In the following code snippet: myinput = document.getElementById("e2"); myinput.style.cssText ='width:100%;height:100%;padding:0px;margin:0px;' div{ ...

Challenges faced while setting up a fresh Angular 7 project

Encountered an error while trying to create a new project using angular cli. I attempted npm clear cache --force and manually deleted the npm cache folder, but neither solution resolved the issue. No proxy is needed for internet connection Ran command: n ...

Designing a function for downloading QR images via the goqr.me API

I'm currently working on integrating dynamically generated QR Code images into my website using the API. I want to offer users the option to download the image by clicking a button, utilizing the download attribute in a hyperlink. However, instead o ...