avatarSebastiano Schwarz

Summary

The article outlines how to create a toast notification service in an Angular 14 application using Bootstrap 5 components.

Abstract

The article titled "How to Create a Toast Service Using Angular 14 and Bootstrap 5" introduces the concept of toast notifications as a user-friendly way to display alerts in web applications. It describes the implementation of a toast service in Angular 14, which includes three main components: the ToastService for managing toast events, the ToasterComponent for stacking and displaying toasts, and the ToastComponent for presenting individual notifications. The service utilizes RxJS Observables for handling toast events. The article also provides a live preview of the Angular Toaster Application, source code on GitHub, and a deployed version on Vercel for readers to explore the functionality. Additionally, the author promotes an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus.

Opinions

  • The author suggests that toast notifications are a common UX pattern for informing users in web applications.
  • The use of RxJS Observables in the ToastService is recommended for handling toast events, indicating a preference for reactive programming patterns.
  • The article implies that the implementation of the toast service is straightforward and can be easily integrated into an Angular application.
  • The author emphasizes the importance of user experience by mentioning that error toasts do not disappear automatically, unlike other types of toasts.
  • By providing a live version of the application and full source code, the author demonstrates confidence in the code quality and usability of the toast service implementation.
  • The promotion of ZAI.chat at the end of the article suggests that the author believes it to be a valuable resource for developers looking for AI services.

How to Create a Toast Service Using Angular 14 and Bootstrap 5

Introduce a common notification pattern to inform your users

Angular 14 toaster with Bootstrap 5 toast notifications (original from Pixabay under Pixabay License)

In most web applications there is a requirement to display some kind of notifications to the user. A common UX pattern for this is so-called toast notifications.

A toast in this context can be simply explained as a piece of information that pops up somewhere on the screen. In this post, we want to have a look on how to create a service for sending Bootstrap 5 toast notifications using Angular 14.

Structure of the Toaster

Our Angular Toaster consists of the following three parts:

  • ToastService that provides the methods for sending the toasts
  • ToasterComponent that takes care of managing and stacking current toasts
  • ToastComponent that presents a single toast notification
Preview of the Angular Toaster Application

Creating the Toast Service

First, we need to create the toast service that we can later call from our application to send different types of toasts.

For the service, we use RxJS to handle our toast events using Observables.

Building the Toaster Component

The toaster component takes care of managing and stacking the toasts. For this the toaster subscribes to our previously created toast service.

In the template of the component, we iterate over all current toast notifications to display them stacked among each other.

The toaster component should be included in the app.component.html of the Angular application to ensure toast notifications are always displayed when a toast event is fired.

Define and style the Toasts

Last but not least, we need to take care of the individual toasts themselves. A single toast mainly defines the methods to show and hide the notification. A small special feature at this point are the error toasts, because we don’t let them disappear automatically like the rest of the toasts do.

Regarding the styling of the toasts, you can also refer to the Bootstrap 5 Toast Documentation. Otherwise, you can also find the link to the full source code below.

The full source code is available on GitHub:

Deployed application on Vercel

To check out the running toaster application, there is a live version deployed on Vercel:

https://angular-bootstrap-toast-service.vercel.app/

Angular
Typescript
Web Development
Bootstrap
Toast
Recommended from ReadMedium