Angular 16 toastr notification; Through this tutorial, i am going to show you how to install and use toaster notification in Angular 16 apps.
Angular 16 Toastr Notifications Tutorial with Example
Follow the below given steps to install and use toaster notification in Angular 16 apps; as follows:
- Step 1 – Create New Angular App
- Step 2 – Install Toaster Notification
- Step 3 – Import Modules in Module.ts File
- Step 4 – Create HMTL on View File
- Step 5 – Import Components in app.Component ts File
- Step 6 – Create Service For Notification
- Step 7 – Start the Angular App
Step 1 – Create New Angular App
Run the following command on command prompt to install angular app:
ng new my-new-app
Step 2 – Install Toaster Notification
Then install NPM package called npm install ngx-toastr –save for implement toaster notification in angular app. So, You can install the packages by executing the following commands on the terminal:
npm install ngx-toastr --save npm install @angular/animations --save
After that, open angular.json file and update the following code into it:
..... "styles": [ "node_modules/ngx-toastr/toastr.css", "src/styles.css" ], .....
Step 3 – Import Modules in Module.ts File
Go to src/app directory and open app.module.ts file. And then import the following lines of code into app.module.ts file:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { ToastrModule } from 'ngx-toastr'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, ToastrModule.forRoot() ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Step 4 – Create HMTL on View File
Go to src/app/ and app.component.html and update the following code into it:
<h1>Angular 13 Toastr Notifications Example - Laratutorials.com</h1> <button (click)="showToasterSuccess()"> Success Toaster </button> <button (click)="showToasterError()"> Error Toaster </button> <button (click)="showToasterInfo()"> Info Toaster </button> <button (click)="showToasterWarning()"> Warning Toaster </button>
Step 5 – Import Components On app.Component ts File
Go to src/app directory and open app.component.ts. Then import the following code into component.ts file:
import { Component } from '@angular/core'; import { NotificationService } from './notification.service' @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'toaster-not'; constructor(private notifyService : NotificationService) { } showToasterSuccess(){ this.notifyService.showSuccess("Data shown successfully !!", "lara.com") } showToasterError(){ this.notifyService.showError("Something is wrong", "lara.com") } showToasterInfo(){ this.notifyService.showInfo("This is info", "lara.com") } showToasterWarning(){ this.notifyService.showWarning("This is warning", "lara.com") } }
Step 6 – Create Service For Notification
Run the following command on command prompt to create services for notifications:
ng generate service notification
Then visit the src/app directory and open notification.service.ts. Then add the following code into notification.service.ts file:
import { Injectable } from '@angular/core'; import { ToastrService } from 'ngx-toastr'; @Injectable({ providedIn: 'root' }) export class NotificationService { constructor(private toastr: ToastrService) { } showSuccess(message, title){ this.toastr.success(message, title) } showError(message, title){ this.toastr.error(message, title) } showInfo(message, title){ this.toastr.info(message, title) } showWarning(message, title){ this.toastr.warning(message, title) } }
Step 7 – Start the Angular App
Run the following command on command prompt to start angular app:
ng serve