Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap Modals
A flexible and highly configurable dialogue prompt, the ngx-bootstrap modal component provides several defaults and can be used with minimum code.
selector
Inputs
Outputs
Methods
We have to update app.module.ts used in the ngx-bootstrap Dropdowns chapter to use ModalModule and BsModalService when we're going to use a modal.
To use the ModalModule and the BsModalService, update app.module.ts.
app.module.ts
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { AppComponent } from './app.component'; import { TestComponent } from './test/test.component'; import { AccordionModule } from 'ngx-bootstrap/accordion'; import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert'; import { ButtonsModule } from 'ngx-bootstrap/buttons'; import { FormsModule } from '@angular/forms'; import { CarouselModule } from 'ngx-bootstrap/carousel'; import { CollapseModule } from 'ngx-bootstrap/collapse'; import { BsDatepickerModule, BsDatepickerConfig } from 'ngx-bootstrap/datepicker'; import { BsDropdownModule,BsDropdownConfig } from 'ngx-bootstrap/dropdown'; import { ModalModule, BsModalService } from 'ngx-bootstrap/modal'; @NgModule({ declarations: [ AppComponent, TestComponent ], imports: [ BrowserAnimationsModule, BrowserModule, AccordionModule, AlertModule, ButtonsModule, FormsModule, CarouselModule, CollapseModule, BsDatepickerModule.forRoot(), BsDropdownModule, ModalModule ], providers: [AlertConfig, BsDatepickerConfig, BsDropdownConfig,BsModalService], bootstrap: [AppComponent] }) export class AppModule { }
Update test.component.html to use the modal.
test.component.html
<button type="button" class="btn btn-primary" (click)="openModal(template)">Open modal</button> <ng-template #template> <div class="modal-header"> <h4 class="modal-title pull-left">Modal</h4> <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> This is a sample modal dialog box. </div> <div class="modal-footer"> <button type="button" class="btn btn-default" (click)="modalRef.hide()">Close</button> </div> </ng-template>
Update test.component.ts for corresponding variables and methods.
test.component.ts
import { Component, OnInit, TemplateRef } from '@angular/core'; import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] }) export class TestComponent implements OnInit { modalRef: BsModalRef; constructor(private modalService: BsModalService) {} openModal(template: TemplateRef) { this.modalRef = this.modalService.show(template); } ngOnInit(): void { } }
Run the following command to start the angular server.
ng serve