Angular 14 Toastr Notifications Example

Angular 14 Toastr Notifications Example

Angular 14 toastr notification; Through this tutorial, we will learn how to integrate and use toaster notification in angular 14 apps.

Angular 14 Toastr Notifications Example

Use the following steps to integrate and use toaster notification in angular 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 – Add Code On app.Component ts File
  • Step 6 – Create Service For Notification
  • Step 7 – Start the Angular App

Step 1 – Create New Angular App

First of all, open your terminal and execute the following command on it 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

In this step, visit src/app directory and open app.module.ts file. And then add the following lines of 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

Create buttons for toaster notification in the angular app. So, visit src/app/ and app.component.html and update the following code into it:

<h1>Angular 14 Toastr Notifications Example - Tutsmake.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 – Add Code On app.Component ts File

Visit the src/app directory and open app.component.ts. Then add 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 !!", "tutsmake.com")
  }
  
  showToasterError(){
      this.notifyService.showError("Something is wrong", "tutsmake.com")
  }
  
  showToasterInfo(){
      this.notifyService.showInfo("This is info", "tutsmake.com")
  }
  
  showToasterWarning(){
      this.notifyService.showWarning("This is warning", "tutsmake.com")
  }
}

Step 6 – Create Service For Notification

In this step, open your terminal and execute the following command on it:

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

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

ng serve

Recommended Angular Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *