Render template in toast using angular template in Angular Toast component

20 Sep 20223 minutes to read

You can also render the template support in Toast using the Angular ng-template. We need to use this ng-template within the e-toast tag with #template attribute, which is mandatory to render template property. Also you don’t need to use the template property if you used this ng-template.

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 #template>
             <div id="template_toast_ele">
               <ejs-datepicker></ejs-datepicker>
             </div>
            </ng-template>
           </ejs-toast>`
})

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

    onCreate(){
         setTimeout(
        () => {
        this.element.show();
        }, 200);
    }

    btnClick() {
      this.toastShow();
    }

    toastShow() {
      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);