paint-brush
How to Show a Modal Dialog in Angularby@developerpartners
23,926 reads
23,926 reads

How to Show a Modal Dialog in Angular

by Developer PartnersAugust 18th, 2023
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

This article introduces the @developer-partners/ngx-modal-dialog library, which simplifies the creation of modal dialogs in Angular. It covers library installation, demonstrating how to show a modal dialog for adding or editing books using ModalService. Additionally, it explains passing data to modal dialogs and emphasizes the benefits of the library for efficient modal dialog creation in Angular projects.
featured image - How to Show a Modal Dialog in Angular
Developer Partners HackerNoon profile picture


Using modal dialogs in web applications is fairly common. Whether you want to edit a record in a table without navigating to a different page, look up some data, or just show a warning message to your users, using a modal dialog may be a great user experience. Unfortunately, doing that is not very easy in Angular without a third-party library. It requires writing some non-trivial code and understanding of the internal mechanisms of the Angular framework. That is the reason why we developed a library at Developer Partners for showing modal dialogs in Angular. We are going to use our library in this article.


1. Install the Library

We will have to install our @developer-partners/ngx-modal-dialog library via NPM and include that in our Angular modules to be able to use it. Here is the NPM command to install it:


npm install @developer-partners/ngx-modal-dialog


Next, we have to include it in the Angular modules where we have to use modal dialogs. Our demo application is very small, it has only one module called AppModule. We have to include ModalModule from the library in the imports array of our AppModule Angular module:


Including ModalModule in AppModule imports


We are all set. We can start using the modal dialog component in our application.


2. Show a Modal Dialog

As mentioned before, we are going to show our modal dialog when we want to add or edit a book in our list of books. Let's first create a component where we are going to show the list of books. We are going to create a new Angular component and call it BookListComponent.


This is the content of the BookListComponent Typescript class:

The BookListComponent Typescript class code.


Our BookListComponent class uses a hardcoded list of books as the data to show in our table for simplicity. It also needs the ModalService Angular service from the ngx-modal-dialog library to be able to show the modal dialogs that is why we injected it into our constructor.


Next, we will create the HTML file for BookListComponent where we will show the list of books in an HTML table:

HTML code of BookListComponent


The HTML code above is pretty simple. It show the list of books in a table. It also has a button called "Add New Book".


This is what the UI looks like so far:

UI of the table with books



When the "Add New Book" button is clicked, it calls the createBook function in our Typescript class. The createBook function shows the modal dialog for adding a new book to our table. This is the Typescript code of the createBook function:



The function that shows the modal dialog for adding a new book



The createBook function shows the modal dialog by calling the show function of the ModalService class. The show function takes 2 parameters: the class of the Angular component to show inside the modal dialog and the options of modal dialog which are the settings of the dialog such as the title, size, position, and a few other things. The modal dialog needs an Angular component to show in its body. Without that component, our modal dialog would be just an empty panel overlaying the screen. That component is CreateEditBookComponent  which is the first parameter of the show function in the screenshot above.


The CreateEditBookComponent Angular component is going to be responsible for both adding and editing books. We will start by working on the code for adding new books. Here is the HTML code of the CreateEditBookComponent which has only a few fields for entering the ID and title of the book we want to create and has buttons for closing the dialog and saving the data:


HTML of the Angular component for adding new books to the list



This is what the modal dialog UI looks like:

UI of the Create Book modal dialog



Here is the Typescript code of the CreateEditBookComponent component:

Angular component for adding new books to the list



The CreateEditBookComponent component uses an Angular service called ModalReference from the ngx-modal-dialog library. We use that service for interacting with the modal dialog where our component is placed such as closing the modal or subscribing to its events. We simply close the modal dialog in the cancel function in the screenshot above. We call the cancel function when the "Cancel" button from HTML is clicked. When we click the "Save" button in the HTML code, it submits the form which calls the saveData function. In the saveData function, we close the modal dialog just like in the cancel function but we also pass the book property to it that contains the data for adding a new book to our list.


The ModalReference service is a generic type. When we call the closeSuccess function of the ModalReference service, we have to pass an object of the same type as its generic parameter. In our case, it's a Typescript interface called Book. The parameter that we pass to the closeSuccess function is passed back to the component that created the modal dialog by calling the show function of the ModalService class.


Newly created book passed back to BookListComponent



When the call the closeSuccess function of the ModalReference service, it closes the modal dialog and triggers an RxJS event passing the newly create book to the subscribers of that event. In the screenshot above, the newBook parameter of our callback function is the newly created book that we received from the modal dialog, so we simply add it to our books array to show it in the UI.


3. Passing Data to Modal Dialogs

There are some cases where you need to pass some data to modal dialogs. For example, if we want to edit a book from the list, we can pass the book record that we want to edit to the modal dialog to have the initial data that we want to modify.


Let's start by adding a button to the table rows for editing the row data:

The edit button for each table row in the books table



This is what the UI looks like with the Edit button in each row of the table:

UI of the book list with Edit button in each row



The editBook function has almost the same code as the createBook function with just one important difference. It passes the book that we want to edit to the modal dialog by using the model property of the of the dialog options.


The function that shows the modal dialog for editing a book


The parameter that we pass to the modal dialog using the model property becomes available in the ModalReference service in the component used inside the dialog via its config.model property.


Using the passed book parameter in the modal dialog for editing



In the screenshot above, we are copying the passed parameter and assigning it to the book property of the CreateEditBookComponent component. The model property of ModalReference.config object is passed by reference, so any changes we make in the properties of that object will be reflected in the table where we show the list of books. The reason why we copy it is to not modify the row in the table until the user clicks the "Save" button. If the user clicks the "Cancel" button, the table data will not be updated.


This is what the Edit Book modal dialog looks like:

UI of the Edit Book modal dialog


Conclusion

Building a modal dialog for Angular from scratch is not easy, but it is much easier with our modal dialog library. We went through how to set up the library and use it for showing modal dialogs in your project. We went through most basic use cases and features of the dialogs, but the @developer-partners/ngx-modal-dialog library has a lot of other features that you may find useful in your real projects.


Also published here.


Please follow the link below to learn more about it:

@developer-partners/ngx-modal-dialog


In case if you would like to show a nice loading spinner in your modal dialogs, please see our article about building a loading spinner in Angular:

How to Create a Loading Spinner in Angular