Introduction

Modal dialogs are a common UI pattern in web applications, allowing you to display important information, forms, or interactions without navigating away from the current page. In Angular, creating and displaying modal dialogs can be achieved with ease, thanks to the framework’s robust capabilities. In this article, we’ll walk you through the process of showing a modal dialog in Angular, complete with coding examples.

Prerequisites

Before we dive into the code, make sure you have the following prerequisites in place:

  1. Angular CLI: If you haven’t already, install the Angular CLI globally on your system. You can do this by running the following command in your terminal:
    shell
    npm install -g @angular/cli
  2. Angular Project: Create a new Angular project or use an existing one. You can generate a new project using the following command:
    shell
    ng new my-modal-project
  3. Angular Material: We’ll use Angular Material to create the modal dialog. If your project doesn’t already include Angular Material, install it using:
    shell
    ng add @angular/material

With these prerequisites in place, let’s proceed to create a modal dialog in Angular.

Step 1: Create a Component for the Modal Dialog

Start by generating a new component that will represent your modal dialog. Use the Angular CLI to create the component:

shell
ng generate component modal

This command will generate a new folder called modal with the necessary files for your modal dialog component.

Step 2: Design the Modal Dialog Component

In the modal component folder, you’ll find several files, including modal.component.html, modal.component.ts, and modal.component.css. Let’s design our modal dialog’s content in the HTML file (modal.component.html).

html
<!-- modal.component.html -->
<div class="modal-overlay">
<div class="modal-dialog">
<h2>Modal Dialog</h2>
<p>This is a simple modal dialog example.</p>
<button (click)="closeModal()">Close</button>
</div>
</div>

In this example, we have a simple modal dialog with a title, a message, and a “Close” button. We’ve also added a click event binding to the “Close” button, which we’ll implement in the TypeScript file to close the modal.

Step 3: Style the Modal Dialog

Add some basic styles to your modal dialog by modifying the modal.component.css file.

css
/* modal.component.css */
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
display: flex;
justify-content: center;
align-items: center;
}
.modal-dialog {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
text-align: center;
}

These styles create a semi-transparent overlay with a centered modal dialog box. Feel free to customize the styles to match your application’s design.

Step 4: Implement the Modal Service

To control the visibility of the modal dialog from other parts of your application, let’s create a modal service. Run the following command to generate a service:

shell
ng generate service modal

Now, open the modal.service.ts file and define a method to show and hide the modal dialog.

typescript
// modal.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: ‘root’,
})
export class ModalService {
private isModalOpen = false;openModal() {
this.isModalOpen = true;
}closeModal() {
this.isModalOpen = false;
}isModalVisible() {
return this.isModalOpen;
}
}

In this service, we maintain a boolean variable isModalOpen to keep track of the modal’s visibility. The openModal() and closeModal() methods allow us to control the modal’s state, and isModalVisible() lets us check if the modal is currently open.

Step 5: Modify the App Component

Now, let’s use the modal service to display the modal dialog from our main app component. Open the app.component.html file and add a button to trigger the modal.

html
<!-- app.component.html -->
<div class="app-container">
<h1>Welcome to My Angular Modal Example</h1>
<button (click)="openModal()">Open Modal</button>
</div>
<app-modal *ngIf=“modalService.isModalVisible()”></app-modal>

In this code, we’ve added a button that calls the openModal() method when clicked. We also use *ngIf to conditionally render the <app-modal> component based on the modal’s visibility state.

Step 6: Implement Modal Actions

Now, let’s implement the modal actions in the app.component.ts file.

typescript
// app.component.ts
import { Component } from '@angular/core';
import { ModalService } from './modal.service';
@Component({
selector: ‘app-root’,
templateUrl: ‘./app.component.html’,
styleUrls: [‘./app.component.css’],
})
export class AppComponent {
constructor(public modalService: ModalService) {}openModal() {
this.modalService.openModal();
}
}

In the AppComponent class, we inject the ModalService and use the openModal() method to open the modal when the button is clicked.

Step 7: Close the Modal

To close the modal when the “Close” button is clicked, we need to make some modifications to the modal.component.ts file.

typescript
// modal.component.ts
import { Component } from '@angular/core';
import { ModalService } from '../modal.service';
@Component({
selector: ‘app-modal’,
templateUrl: ‘./modal.component.html’,
styleUrls: [‘./modal.component.css’],
})
export class ModalComponent {
constructor(public modalService: ModalService) {}closeModal() {
this.modalService.closeModal();
}
}

In the ModalComponent class, we inject the ModalService and use the closeModal() method to close the modal when the “Close” button is clicked.

Step 8: Test Your Modal Dialog

With everything set up, it’s time to test your modal dialog. Run your Angular application using the following command:

shell
ng serve

Open your web browser and navigate to http://localhost:4200. Click the “Open Modal” button, and you should see your modal dialog displayed on top of the main content. Click the “Close” button to hide the modal.

Conclusion

In this tutorial, we’ve walked through the process of creating and displaying a modal dialog in an Angular application. We covered creating a modal dialog component, designing its content and styles, implementing a modal service, and integrating the modal into the main app component. With these steps, you can easily add modal dialogs to enhance the user experience in your Angular applications. Feel free to customize and expand upon this example to suit your specific requirements. Happy coding!