Add dynamic template in Angular Toast component
27 Apr 20243 minutes to read
Toast can support to change templates in dynamically, with displaying in multiple toasts. We can change Toast properties while calling in show
method.
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 { Component, ViewChild } from '@angular/core';
@Component({
imports: [
ToastModule, ButtonModule, CheckBoxModule , RadioButtonModule, DropDownListModule, DatePickerModule
],
standalone: true,
selector: 'app-root',
template: `
<div id="toast_target"></div>
<div id='templateToast' style="display: none;color:red"> System affected by virus !!! </div>
<button ejs-button [isPrimary]="true" (click)="btnClick($event)">Show Toast</button>
<ejs-toast #element (created)="onCreate($event)" [position] = 'position' > </ejs-toast>
`
})
export class AppComponent {
@ViewChild('element') public element: any;
public position = { X: 'Right', Y: 'Bottom' };
public toasts = [ { template: '2 Mail has received'},{ template: 'User Guest Logged in'},{ template: 'Logging in as Guest'},{ template: 'Ticket has reserved '},{ template: '#templateToast' }]
public toastFlag: number = 0;
onCreate(args: any) {
this.element.show(this.toasts[this.toastFlag]);
++this.toastFlag;
}
btnClick(args: any) {
this.toastShow();
}
toastShow() {
setTimeout(
() => {
this.element.show(this.toasts[this.toastFlag]);
++this.toastFlag;
if (this.toastFlag === (this.toasts.length)) {
this.toastFlag = 0;
}
}, 0);
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));