0

I have a sample Angular 12 project with Bootstrap 4. Everything is working in my project except the dropdown.

This SO answer was not working for me.

I have installed jQuery, Popper and Bootstrap in my project using the NPM commands given :

npm install --save @popperjs/core
npm install --save bootstrap
npm install --save jquery

I also referred them from the angular.json :

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

I'm getting no error and also dropdown button is coming. But the issue is that the list is not popping up as soon as I clicked.

The sample code is given below that I took from Bootstrap Dropdown Official Docs for testing.

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <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>
</div>

Am I missing something which need to be added ?

2
  • When I run npm install --save bootstrap it installs Bootstrap 5 so the sample code for the dropdown button supplied above won't work. Try the following code for the dropdown button Commented May 28, 2021 at 16:07
  • stackblitz.com/edit/… here is your working example @AnishB. Commented May 28, 2021 at 16:35

2 Answers 2

4

it worked for me

"scripts": [
     "node_modules/jquery/dist/jquery.min.js", 
     "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
]

Edit : omg. this issue was very tiring for me. i worked on it for a few hours. and the problem was solved in an unnoticed way and i don't understand the cause. i had to read bootstrap.js for this lol. just change the expression data-toggle="dropdown" to data-bs-toggle="dropdown" in the html file.

so :

in html

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <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>
</div>

Note: The angular.json example i shared above is still running.

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

8 Comments

Let me try this.
nothing seems wrong. did you restart the application with "ng serve" after refreshing the script sequence?
Yes ! restarted everything. Still no success. Did you try with Angular 12 ?
i am using angular 11. i am going to open a new project and try it
i updated the comment and yes it solved the problem
|
0

Yep solution worked for me thanks!

However I didn't update angular.json as per above, instead added the bootstrap dropdown module in app.module.ts i.e:

import { BsDropdownModule } from 'ngx-bootstrap/dropdown';

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

Comments

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.