In this article, we are going to understand the usage of Bootstrap(V5) Modal in Angular(V13) applications.
Now 'angular.json' file configure boostrap 'CSS', 'JS' references.
Create An Angular Application:
Let's create an angular application to accomplish our demo.
Command To Install Angular CLI:
npm install -g @angular/cli
npm install -g @angular/cli
Command To Create Angular App:
ng new your_app_name
ng new your_app_name
Install And Configure Boostrap(V5)
Install the Bootstrap NPM package.
npm install bootstrap
Now 'angular.json' file configure boostrap 'CSS', 'JS' references.
Bootstrap Modal Usage:
Let's implement a small sample to understand the usage of Bootstrap Modal.
src/app.component.ts:
import { Component, OnInit } from '@angular/core'; import { AppService } from './app.service'; declare var window: any; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent implements OnInit { formModal: any; constructor() {} ngOnInit(): void { this.formModal = new window.bootstrap.Modal( document.getElementById('myModal') ); } openFormModal() { this.formModal.show(); } saveSomeThing() { // confirm or save something this.formModal.hide(); } }
- (Line: 3) Declare the 'window', to access the window instance.
- (Line: 11) Declare a variable like 'formModal', which going to contain the instance of Bootstrap modal.
- (Line: 16-18) Here initialized the 'bootstrap.Modal()' and then assigned to 'formModal' variable.
- Get the bootstrap modal HTML element to pass as an input parameter.
- (Line: 22) The 'show()' method to open modal.
- (Line: 26) The 'hide()' method to close modal.
<nav class="navbar navbar-expand-lg navbar-light bg-primary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Bootstrap 5</a> </div> </nav> <div class="container"> <div class="row mt-4 mb-2 text-center"> <div class="col"> <button type="button" (click)="openFormModal()" class="btn btn-dark"> Add </button> </div> </div> </div> <!--form modal--> <div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" > <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Bootstrp Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" ></button> </div> <div class="modal-body"> <h3>Hello!</h3> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"> Close </button> <button type="button" class="btn btn-primary" (click)="saveSomeThing()"> Save changes </button> </div> </div> </div> </div>
- (Line: 10-12) The 'Add' button registered with click event 'openFormModal()'.
- (Line: 20) The 'id' value of the 'Modal' will be used in our 'ts' file for interacting with the bootstrap instance.
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on Bootstrap(V5) Modal in Angular application. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Support Me!
Buy Me A Coffee
PayPal Me
how to disable outside click on close the modal?
ReplyDeleteThank you very much sir, it was really helpfull and worked like a charm.
ReplyDeleteVery nice, it works with angular 15 and bootstrap 5 too!!!!
ReplyDeleteThank you Worked great for me. At first, I tried to use the hide only which was a problem because of different reference and pointers but finally used both the open and hide and that worked well
ReplyDelete