Toaster Notification Example Angular 11/12

We will learn the implementation of toaster notification in Angular 10/11/12.

This Angular tutorial is compatible with version 4+ including latest version 12, 11, 10, 9, 8 ,7, 6 & 5.

In addition to this, we will also get to know how to use toaster notification in Angular 11/12 using ngx-toastr npm package for toaster notification. Also, two npm packages ngx-toastr and @angular/animations that provide to use success, error, warning and info alert messages.

Learn to use Toaster Notification in Angular 11/12

We simply need to follow the given steps for implementing the Toaster Notification in Angular 12 app:

 

Step 1 is to Create the New Angular App

Step 2 is to Install the Toaster Notification

Step 3 is to Add Code on App.Module.ts File

Step 4 is to Add Code on View File

Step 5 is to Add Code On app.Component ts File

Step 6 is to Create Service for Notification

Step 7 is to Start the Angular App

 

Now, let’s learn in detail

Step 1- Creating the New Angular App

First of all, open your terminal and execute the given command on it to install an angular app:

ng new toaster-notification-app

Step 2 : Installing the Toatser Notification 

Next, just install the npm package called npm install ngx-toastr -save for implementing the toaster notification in an Angular app. Further, we can install the packages by executing the following commands on the terminal

npm install ngx-toastr --save
npm install @angular/animations --save

Thereafter, we open the angular.json file and update the following code into it:

.....
    "styles": [
      "node_modules/ngx-toastr/toastr.css",
      "src/styles.css"
    ],
.....

Step 3: Adding Code on App.Module.ts File

Afterwards, we visit src/app directory and open app.module.ts file. Thereafter, we also add the following lines 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: Adding the Code on View File

In next step, we create the owl carousel in the Angular 11 app. Then, we visit src/app/ and app.component.html and update the following code into it:

<h1>Angular 11 Toastr Notifications Example - Thecodemon.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: Adding Code On app.Component ts File

Now, follow this step in which we visit the src/app directory and open app.component.ts.  After this, we 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 !!", "ItSolutionStuff.com")
  }
   
  showToasterError(){
      this.notifyService.showError("Something is wrong", "ItSolutionStuff.com")
  }
   
  showToasterInfo(){
      this.notifyService.showInfo("This is info", "ItSolutionStuff.com")
  }
   
  showToasterWarning(){
      this.notifyService.showWarning("This is warning", "ItSolutionStuff.com")
  }
}

Step 6: Create Service for Notification

In this step, we open the terminal and execute the following command into it:

ng generate service notification

Now, we visit the src/app directory and open notification.service.ts.  After this, we 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)
  }
   
}

Starting the Angular App

Lastly, we reach the final step of implementation where we execute the following command executed on the terminal to start angular owl carousel app:

ng serve

Conclusion

Finally, we reach the end of explaining implementation of Toaster Notification in Angular 12 app. Hope you have well understood the concept. 

Thanks

Leave a Reply

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