Toaster Notification in Angular 16

Toaster notifications are a great way to provide feedback to users in an Angular application.

To use toaster notifications in Angular 16, you will need to install the ngx-toastr package.

Once installed, you can import the ToastrModule into your application’s root module.

In the component where you want to display the toaster notification, you can then inject the ToastrService and use the success(), error(), warning(), and info() methods to display the notifications.

For example:

import { ToastrService } from ‘ngx-toastr’;

constructor(private toastr: ToastrService) { }

// Display success notification
this.toastr.success(‘Your message’, ‘Title’);

// Display error notification
this.toastr.error(‘Your message’, ‘Title’);

// Display warning notification
this.toastr.warning(‘Your message’, ‘Title’);

// Display info notification
this.toastr.info(‘Your message’, ‘Title’);
[ad_1]

Toasters are a fantastic way to provide feedback and notifications to users in a user-friendly manner. Feel free to experiment further with customizations and explore more features offered by the ngx-toastr library to create a delightful user experience in angular projects.

In this tutorial, you will learn how to integrate, use and customize toaster notifications in angular 16 projects using ngx-toastr.

How to Install and Use Toaster Notification in Angular 16

Steps to integrate and use toaster notifications in angular project using ngx-toastr; are as follows:

  • Step 1 – Create New Angular App
  • Step 2 – Install ngx-toastr package
  • Step 3 – Import and configure the ToastrModule
  • Step 4 – Implement the toaster service

  • Step 5 – Create a button to trigger the toaster
  • Step 6 – Implement the showToaster() method
  • Step 7 – Customize the toaster appearance (optional)
  • Step 8 – Start the Angular App

Step 1 – Create New Angular App

First of all, open your command prompt or cmd to install & setup angular new project in system by executing the following command on command prompt or cmd:

ng new my-new-app

Step 2 – Install ngx-toastr package

Next, Open a command prompt or cmd and navigate to your Angular project folder. Install ngx-toastr using npm:

npm install ngx-toastr --save

Step 3 – Import and configure the ToastrModule

In the app.module.ts file, import the necessary modules:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule } from 'ngx-toastr';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    ToastrModule.forRoot(),
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }

Step 4 – Implement the toaster service

Create a new service to manage toaster messages. In the terminal, run:

ng generate service toaster

Open the generated toaster.service.ts file and add the following code:

import { Injectable } from '@angular/core';
import { ToastrService } from 'ngx-toastr';

@Injectable({
  providedIn: 'root',
})
export class ToasterService {

  constructor(private toastr: ToastrService) { }

  success(message: string, title?: string) {
    this.toastr.success(message, title);
  }

  error(message: string, title?: string) {
    this.toastr.error(message, title);
  }

  warning(message: string, title?: string) {
    this.toastr.warning(message, title);
  }

  info(message: string, title?: string) {
    this.toastr.info(message, title);
  }

}

Step 5 – Create a button to trigger the toaster

In the app.component.html file, add a button to trigger the toaster:

<button (click)="showToaster()" class="btn btn-primary">Show Toaster</button>

Step 6 – Implement the showToaster() method

In the app.component.ts file, import the ToasterService and create the showToaster() method:

import { Component } from '@angular/core';
import { ToasterService } from './toaster.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  constructor(private toasterService: ToasterService) { }

  showToaster() {
    this.toasterService.success('This is a success message!', 'Success');
  }
}

Step 7 – Customize the toaster appearance (optional)

You can customize the appearance of the toasters by configuring the ToastrModule in app.module.ts. The ToastrModule.forRoot() method accepts a configuration object. Here’s an example of how to customize the toasters:

@NgModule({
  // ...
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    ToastrModule.forRoot({
      timeOut: 3000, // Time to close the toaster (in milliseconds)
      positionClass: 'toast-top-right', // Toast position
      closeButton: true, // Show close button
      progressBar: true, // Show progress bar
    }),
  ],
  // ...
})
export class AppModule { }

Step 8 – Start the Angular App

In this step, execute the following command on terminal to start angular app:

ng serve

Conclusion

Congratulations! You have successfully implemented and customized toasters in your Angular application using the ngx-toastr library.

Recommended Angular Tutorials

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00