This section briefly explains about how to create a simple Toast and configure its available functionalities in Angular using Angular quickstart.
The following section explains the steps required to create a simple angular-cli
application and how to configure Toast
component.
To get started with Syncfusion Angular UI Components, make sure that you have compatible versions of Angular and TypeScript.
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
ng new syncfusion-angular-app
Once you have executed the above command you may ask for following options,
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
Syncfusion packages are distributed in npm as @syncfusion
scoped packages. You can get all the angular syncfusion package from npm link.
Add Syncfusion Angular notifications package in your application by using following Command,
npm install @syncfusion/ej2-angular-notifications --save
(or)
npm i @syncfusion/ej2-angular-notifications --save
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 { }
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() {
}
}
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.
The Toast message can be rendered by defining an title
or content
.
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') element;
public position = { X: 'Right' };
onCreate() {
this.element.show();
}
btnClick() {
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';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
<!DOCTYPE html>
<html lang="en">
<head>
<title>Syncfusion Angular Toast Sample</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript Toolbar Controls" />
<meta name="author" content="Syncfusion" />
<link href="//cdn.syncfusion.com/ej2/material.css" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="index.css" rel="stylesheet" />
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zone.js/0.9.0/zone.min.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<app-root>
<div id='loader'>LOADING....</div>
</app-root>
<script id="template_toast_ele" type="text/x-template">
<div id='template_toast' style="display: none">
<div class="horizontal-align">
<div class='toast-content'>
<div class='toast-title'>
Weekend Alarm
</div>
<div class='toast-message'> With traffic, its likely to take 45 minutes to get to jenny's 24th Birthday Bash at Hillside Bar, 454 E.
Olive Way by 10:00PM </div>
</div>
</div>
<img src="https://ej2.syncfusion.com/demos/src/toast/resource/map.jpg" width="100%" height="70%">
</div>
</script>
</body>
</html>
/* csslint ignore:start */
#container {
margin: 25px;
}
#elementToastTime .e-toast {
width: 500px;
height: 147px;
}
#elementToastTime {
text-align: center;
}
#elementToastTime img {
width: 100px;
height: 100px;
border-radius: 50%;
}
#elementToastTime .e-toast-message {
width: inherit;
}
#sample_container {
margin: 10px;
}
body>#element .e-toast {
width: 400px !important;
}
#template_toast .horizontal-align {
margin: 10px 0;
}
#template_toast .horizontal-align .toast-content .toast-message {
opacity: 0.4;
}
#template_toast .horizontal-align .toast-content {
display: inline-flex;
flex: 1;
flex-direction: column;
margin-left: 10px;
}
#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.progress {
height: 20px;
position: relative;
margin: 20px 0 20px 0;
background: #555;
-moz-border-radius: 25px;
-webkit-border-radius: 25px;
border-radius: 25px;
box-shadow: inset 0 -1px 1px rgba(255, 255, 255, 0.3);
}
.progress span {
background-color: #f0a3a3;
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #f0a3a3), color-stop(1, #f42323));
display: block;
height: 100%;
border-radius: 10px;
width: 50%;
position: relative;
overflow: hidden;
}
.progress span::after {
background-image: -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.25, rgba(255, 255, 255, .2)), color-stop(.25, transparent), color-stop(.5, transparent), color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.75, rgba(255, 255, 255, .2)), color-stop(.75, transparent), to(transparent));
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background-size: 50px 50px;
-webkit-animation: moveAnimate 2s linear infinite;
overflow: hidden;
}
@-webkit-keyframes moveAnimate {
0% {
background-position: 0 0;
}
100% {
background-position: 50px 50px;
}
}
/* csslint ignore:end */