Articles in this section
Category / Section

How to get started easily with Syncfusion Angular 9 Toast notification?

2 mins read

The Essential JS 2 Angular Toast is small and nonblocking pop-up. A small toast message is shown to users at the bottom of the screen or at a specific target and disappears automatically after a few seconds (time-out). In this knowledge base, we are going to provide details about how to easily integrate Syncfusion Angular Toast in Angular 9 application and how to enable its commonly used features.

Prerequisites

Before start, we need following items to create Angular Toast in Angular 9 application

Installation and application creation

  • Install Angular cli 9 using following command.
          npm install -g @angular/cli@9.0.2
    

Angular CLI application Installation by using Command prompt

Note:

If you would like to follow and run the application in Angular8 or below, you need to replace the CLI command version number with the corresponding angular version number.

 

npm install -g @angular/cli@<CLI VERSION>

  • Create an Angular 9 application using Angular cli.
          ng new angular9-app
          cd angular9-app
    
  • Serve the Angular 9 application using following command
           ng serve
    

Listen the application in localhost:4200. Your application will serve in browser. Refer the below example screenshot for Angular 9 version.

 

Angular 9 application

Integration of Angular Toast

  • After running the Angular 9 application successfully, configure the Angular Toast in this application. Install Angular Toast and EJ2 package using following command. The --save command will instruct the NPM to include a notifications package inside the dependencies section of the package.json.
               npm install @syncfusion/ej2-angular-notifications --save 
               npm install @syncfusion/ej2 --save 
    

          

  • Import Toast from installed package in app/app.module.ts.
    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { ToastModule } from '@syncfusion/ej2-angular-notifications';
    import { AppComponent } from './app.component';
     
     
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        ToastModule
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule { } 
    
  • You should refer the CSS file for Angular Toast in style.CSS     
    @import "../node_modules/@syncfusion/ej2/material.css";
    
  • Add the Angular Toast component in app.component.html.
               <ejs-toast></ejs-toast >
    
  • Now, render the toast component as like below.
    <div id=”defaultToast”>
    <ejs-toast #defaultToast id='defaulttoast' [position]="position" (created)="onCreate()"  icon="e-meeting">
          <ng-template #content>
              <div>Conference Room 01 / Building 135 10:00 AM-10:30 AM</div>
           </ng-template>
            <ng-template #title>
               <div>Adaptive Tiles Meeting</div>
           </ng-template>            
     </ejs-toast>
    <button ejs-button #toastBtnShow class="e-btn" id="toastBtnShow" (click)='showToast($event)'>Show Toasts</button>
     <button ejs-button #toastBtnHide class="e-btn" id="toastBtnHide" (click)='hideToast($event)'>Hide Toast</button>
    <div>
    

 

import { Component, ViewEncapsulation, ViewChild, ElementRef, } from '@angular/core';
import { ToastComponent } from '@syncfusion/ej2-angular-notifications';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
    @ViewChild('defaultToast', {static: true})
     public toastObj: ToastComponent;
    @ViewChild('toastBtnShow')
     public buttonEleShow: ElementRef;
     public position: Object = { X: "Right" };
 
      public onCreate: EmitType<Object> = () => {
          setTimeout(()=>{
           this.toastObj.show();
           },200);
       }
    public showToast: EmitType<Object> = (e: Object) => {
           this.toastObj.show();
    }
     public hideToast: EmitType<Object> = (e: Object) => {
           this.toastObj.hide('All');
      }
}

 

  • Now serve the application using following command.
     ng serve --open
    

Once all the files are compiled successfully. It will serve the site at localhost:4200

The following screenshot illustrates this.

Toast default

Summary

Refer to our documentation and online samples for more features. If you have any queries, please let us know in comments below. You can also contact us through our Support forum or Direct-Trac. We are happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied