Adding Bootstrap to your Angular application is an easy process. Just write the following command in your Angular CLI. It will add bootstrap into your node_modules folder.
ng add @ng-bootstrap/ng-bootstrap
Approach: Import NgbModal module in the TypeScript file of the corresponding component, and then we have to write code for the popup model by using the above module in the HTML file of the corresponding component.
Syntax:
- In typescript file:
import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; - In html file:
<ng-template #content let-modal> ... </ng-template>
Example: modal-basic.ts
import {Component} from '@angular/core'; import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap'; @Component({ selector: 'ngbd-modal-basic', templateUrl: './modal-basic.html'}) export class NgbdModalBasic { closeResult = ''; constructor(private modalService: NgbModal) {} open(content) { this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => { this.closeResult = `Closed with: ${result}`; }, (reason) => { this.closeResult = `Dismissed ${this.getDismissReason(reason)}`; }); } private getDismissReason(reason: any): string { if (reason === ModalDismissReasons.ESC) { return 'by pressing ESC'; } else if (reason === ModalDismissReasons.BACKDROP_CLICK) { return 'by clicking on a backdrop'; } else { return `with: ${reason}`; } } } |
Now, we have to use ng-template to construct the model which will create a popup.
Example: modal-basic.html
<ng-template #content let-modal> <div class="modal-header"> <h4 class="modal-title" id="modal-basic-title"> Geeks of Geeks </h4> <button type="button" class="close" aria-label="Close" (click)= "modal.dismiss('Cross click')"> <span aria-hidden="true"> × </span> </button> </div> <div class="modal-body"> <form> <div class="form-group"> <label for="dateOfBirth"> Date of birth </label> <div class="input-group"> <input id="dateOfBirth" class="form-control" placeholder="yyyy-mm-dd" name="dp" ngbDatepicker #dp="ngbDatepicker"> <div class="input-group-append"> <button class="btn btn-outline-secondary calendar" (click)="dp.toggle()" type="button"> </button> </div> </div> </div> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')"> Save </button> </div> </ng-template> <button class="btn btn-lg btn-outline-primary" (click)="open(content)"> Popup using Angular and Bootstrap </button> |
Output:


Recommended Posts:
- How to use bootstrap 4 in angular 2?
- AngularJS | angular.bootstrap() Function
- How to open dropdown menu on hover in Bootstrap ?
- How to accordion scroll to top to open content in Bootstrap ?
- How to open or close react-bootstrap modal pro-grammatically?
- Adding Angular Material Component to Angular Application
- Angular 7 | Angular Data Services using Observable
- Angular CLI | Angular Project Setup
- Routing in Angular JS using Angular UI Router
- Difference between Angular 4 and Angular 5
- Semantic-UI | Popup
- What are the types of Popup box available in JavaScript ?
- How to use the Magnific Popup jQuery plugin?
- How to center a popup window on screen?
- Include Bootstrap in AngularJS using ng-bootstrap
- Difference between bootstrap.css and bootstrap-theme.css
- How to display popup message when logged out user try to vote ?
- Difference Between Bootstrap 3 and Bootstrap 4
- Difference between Bootstrap 4 and Bootstrap 5
- What does '<?=' short open tag mean in PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

