4

I have included bootstrap 5 in index.html

<link href="assets/css/bootstrap.min.css" rel="stylesheet" media="screen">

I tired to use the sample code from Bootstrap documentation, in my angular page, it's not working.

what could be the reason, is there another way to use modal inside angular page?

3
  • try adding the css in angular.json Commented Jun 23, 2021 at 8:12
  • Added it in angular.json still not responding Commented Jun 23, 2021 at 8:20
  • ok try installing the bootstrap npm i bootstrap Commented Jun 23, 2021 at 8:31

5 Answers 5

6

In 2021 works perfectly,

install bootstrap with npm install --save bootstrap

in angular.json file set route to bootstrap css and js

in component.ts file

import * as bootstrap from 'bootstrap';

  modalName: bootstrap.Modal | undefined
save(){
    this.testModal?.toggle()
  }
  openModal(element){
    this.testModal = new bootstrap.Modal(element,{} ) 
    this.testModal?.show()
  }

in component.html file

<button class="btn btn-primary" (click)="openModal(testModal)">Add dish</button>
    
<div class="modal" tabindex="-1" #testModal id="testModal"> ...</div>
Sign up to request clarification or add additional context in comments.

2 Comments

very interesting, can you please explain a bit more in deep? I tried to understand what u meant but put in a component the compiler says the interface is not implemented properly. since I cant find proper docs about that, would be very useful to get help from this post
Your new boostrap.Modal requires an element. That element is testModal but testModal points back to itself. Would this not get stuck in a loop? Thanks for explanation in advance.
2

Try installing bootstrap 5 npm install bootstrap@next in angular and use bootstrap 5 Modal, it should work https://www.npmjs.com/package/bootstrap/v/5.0.0-alpha1

Comments

2
const a = new (window as any).bootstrap.Modal(element);
 //a.toglle();
 //a.show();
 //a.hide();
 //a.dispose();

2 Comments

Although other solutions are cleaners, adding @types/bootstrap caused me an issue. The only way around it was to use this format of accessing bootstrap javascript code. See stackoverflow.com/questions/69841021/…
this worked for me: const a = new (window as any).bootstrap.Modal("#exampleModal");
2

I tried many other answers and the answer from @Gajanan was the one that worked for me. I answer this for the me of the future or other people that might encounter this problem, it is actually very simple and it might be explained in the Boostrap documentation or the Angular documentation and this is a tl;dr.

This is the process I used:

  1. I started a new Angular project from the cli, using scss and TypeScript.
  2. Then downloaded Bootstrap using NPM.
  3. Then added the Bootstrap .scss file to the default styles scss.

After this my monke brain thought everything was right, because the Bootstrap styles loaded fine. But then I tried to use the modal, and it didn't work so I used the @Gajanan response and it worked.

But to that I have to add. Be careful where you add the script, I added the script elsewhere and I threw like 15 minutes to the garbage.

{
  ...
  "projects": {
    ...
    "myProject": {
      ...
      "architect": {
        ...
        "build": {
          ...
          "options": {
            ...
            "scripts": ["here 🎉"],
          },
          ...
          "test": {
            ...
            "options": {
              ...
              "scripts": ["NOT here"],
            }
          }
        }
      }
    }
  }
}

I know... I know... pretty obvious, but I had that problem, so this is a way of punishing myself for committing that error.

Comments

1

run below commands into our project terminal to install bootstrap 5 modules into our angular application:

npm install bootstrap@next

add below code into src/app/app.component.html file to get final out on the web browser:

  <!-- Button trigger modal -->
   <button type="button" class="btn btn-primary" data-bs-toggle="modal" 
  data-bs-target="#exampleModal">
  Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria- 
  labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title text-danger" id="exampleModalLabel">Hey 
      Bootstrap 5!!</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" 
       aria-label="Close"></button>
      </div>
      <div class="modal-body text-danger">
       Therichpost.com
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-warning" data-bs- 
       dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

need to add the below code into angular.json file:

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

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.