Custom Modal Popup with Parent Component in Angular 12

In this article, we will learn how to use Custom Bootstrap Modal from Parent Component on Button Click to child component.

For this, we will use Angular 12 and Ng-Bootstrap.

You have to follow these below steps,

Let’s Get Started

Step 1: Create New Angular Project, write below code in Cmd

ng new custom-modal-popup

Step 2: Add a new component 

ng g c custom-modal

Step 3: Add ng-bootstrap and Bootstrap

Here I have used ng-bootstrap for modal and Bootstrap for UI.

If you don’t know how to Add Bootstrap then click here

Install ng-bootstrap by adding this command into cmd

npm install --save @ng-bootstrap/ng-bootstrap

  Step 4: Paste the Below code in app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CustomModalComponent } from './custom-modal/custom-modal.component';
import { NgbActiveModal, NgbModule } from '@ng-bootstrap/ng-bootstrap';

  declarations: [
  imports: [
  providers: [ NgbActiveModal],
  bootstrap: [AppComponent]
export class AppModule { }

Import NgbModal by adding the below the line of code in app.component.ts

Here NgbModal is a service for opening modal windows.

import { NgbModal} from '@ng-bootstrap/ng-bootstrap';

Also, import NgbActiveModal in custom-modal.component.ts

Here NgbActiveModal is used For  .close() or .dismiss() the modal window used.

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

Step 5: For Bootstrap Modal  Add below HTML code  in custom-modalcomponent.html

<div class="modal-header">
    <h5 class="modal-title">Modal title</h5>
    <button type="button" class="close" data-dismiss="modal" (click)="activeModal.close('dismiss')">
        <span aria-hidden="true">&times;</span>
<div class="modal-body">
    <p>Modal body text goes here.</p>
<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal"

Step 6: Now, we will add a button in the parent component, so paste these into app.component.html.

<div class="container">
  <div class="row">
    <div class="col text-center">
      <button class="btn btn-lg btn-outline-primary" (click)="openModal()">Launch demo modal</button>

 To display the Modal Add below functionality  in app.component.ts

Here modalRef is a reference to the newly opened modal returned by the method.

closeModal: any;
constructor(public modalService: NgbModal) { }

openModal() {
  const modalRef =,
      scrollable: true,
      windowClass: 'myCustomModalClass',

  modalRef.result.then((result:any) => {
  }, (reason:any) => {

To Modal close  Add below functionality in custom-modal.component.ts

    public activeModal: NgbActiveModal
  ) { }
      ngOnInit(): void {
      closeModal(message: string) {

Run your Angular Project.

1 Comment

  1. mousam



Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories