Getting started with Angular Toast component

19 Jan 20247 minutes to read

This section briefly explains about how to create a simple Toast and configure its available functionalities in Angular using Angular quickstart.

Getting Started with Angular CLI

The following section explains the steps required to create a simple angular-cli application and how to configure Toast component.

Prerequisites

To get started with Syncfusion Angular UI Components, make sure that you have compatible versions of Angular and TypeScript.

  • Angular : 6+
  • TypeScript : 2.6+

Setting up an Angular project

Angular provides an easiest way to setup project using Angular CLI Angular CLI tool.

Install the CLI application globally in your machine.

  npm install -g @angular/cli

Create a new application

  ng new syncfusion-angular-app

Once you have executed the above command you may ask for following options,

  • Would you like to add Angular routing?
  • Which stylesheet format would you like to use?

By default it install the CSS style base application. To setup with SCSS, pass –style=SCSS argument on create project.

Example code snippet.

  ng new syncfusion-angular-app --style=SCSS

Navigate to the created project folder.

  cd syncfusion-angular-app

Installing Syncfusion notifications Package

Syncfusion packages are distributed in npm as @syncfusion scoped packages. You can get all the Angular Syncfusion package from npm link.

Currently, Syncfusion provides two types of package structures for Angular components,

  1. Ivy library distribution package format
  2. Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package.

Ivy library distribution package

Syncfusion Angular packages(>=20.2.36) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.

Add @syncfusion/ej2-angular-notifications package to the application.

npm install @syncfusion/ej2-angular-notifications --save

Angular compatibility compiled package(ngcc)

For Angular version below 12, you can use the legacy (ngcc) package of the Syncfusion Angular components. To download the ngcc package use the below.

Add @syncfusion/ej2-angular-notifications@ngcc package to the application.

npm install @syncfusion/ej2-angular-notifications@ngcc --save

To mention the ngcc package in the package.json file, add the suffix -ngcc with the package version as below.

@syncfusion/ej2-angular-notifications:"20.2.38-ngcc"

Note: If the ngcc tag is not specified while installing the package, the Ivy Library Package will be installed and this package will throw a warning.

Adding Toast module

After installing the package, the component modules are ready to configure in your application from installed syncfusion package. Syncfusion Angular package provides two different types of ngModules.

Refer to Ng-Module to learn about ngModules.

Refer the following snippet to import the ToastModule in app.module.ts from the @syncfusion/ej2-angular-notifications.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
// Imported syncfusion ToastModule from ej2-angular-notifications package
import { ToastModule } from '@syncfusion/ej2-angular-notifications';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    // Registering EJ2 Toast Module
    ToastModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Adding Toast component

Add the Toast component snippet in app.component.ts as follows.

import { Component, ViewChild } from '@angular/core';
import { ToastComponent } from '@syncfusion/ej2-angular-notifications';

@Component({
  selector: 'app-root',
  template: `<ejs-toast #element (created)="onCreate($event)">
    <ng-template #title>
      <div>Sample Toast Title</div>
    </ng-template>
    <ng-template #content>
      <div>Sample Toast Content</div>
    </ng-template>
    </ejs-toast>`
})

export class AppComponent {
  @ViewChild('element') element: ToastComponent;

    onCreate() {
    this.element.show();
  }

  constructor() {
  }
}

Adding CSS reference

The following CSS files are available in ../node_modules/@syncfusion package folder. This can be referenced in [src/styles.css] using following code.

      @import '../node_modules/@syncfusion/ej2-base/styles/material.css';
      @import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
      @import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
      @import '../node_modules/@syncfusion/ej2-angular-notifications/styles/material.css';

The Custom Resource Generator (CRG) is an online web tool, which can be used to generate the custom script and styles for a set of specific components.
This web tool is useful to combine the required component scripts and styles in a single file.

Initialize the Toast with message

The Toast message can be rendered by defining an title or content.

Running the application

Run the ng serve command in command window, it will serve your application and you can open the browser window. Output will appear as follows.

import { Component, ViewChild } from '@angular/core';

@Component({
    selector: 'app-root',
    template: `
       <button ejs-button [isPrimary]="true" (click)="btnClick($event)">Show Toast</button>
        <ejs-toast #element (created)="onCreate($event)" [position]='position'>
              <ng-template #title>
                  <div>Matt sent you a friend request</div>
              </ng-template>
              <ng-template #content>
                  <div>Hey, wanna dress up as wizards and ride our hoverboards?</div>
              </ng-template>
    </ejs-toast>`
})

export class AppComponent {
  @ViewChild('element') public element: any;
  public position = { X: 'Right' };

  onCreate(args: any) {
    this.element.show();
  }

  btnClick(args: any) {
    this.element.show();
  }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ToastModule } from '@syncfusion/ej2-angular-notifications';
import { ButtonModule, CheckBoxModule , RadioButtonModule } from '@syncfusion/ej2-angular-buttons';
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns';
import { DatePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, ToastModule, ButtonModule, CheckBoxModule , RadioButtonModule, DropDownListModule, DatePickerModule
    ],
    declarations: [AppComponent ],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

See Also