Template in Angular Message component

14 Nov 20223 minutes to read

The message supports templates that allows the user to customize the content with a custom structure. The content can be a string, paragraph, or any other HTML element. The template can be rendered through the content property or added directly to the HTML element.

In the following sample, the Message component content is customized with HTML elements and Angular Button components, which are directly added to the HTML element.

import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons';
import { MessageComponent } from '@syncfusion/ej2-angular-notifications';

@Component({
    selector: 'app-root',
    template: `<div class="msg-template-section">
        <div class="content-section">
            <button ejs-button #showBtn id='showBtn' content='Show pull request' cssClass="e-outline e-primary e-success msg-hidden"
            (click)="showClick()"></button>
            <ejs-message #msg_template id="msg_template" severity="Success" (closed)="closed()">
                <ng-template #content>
                    <h1>Merged pull request</h1>
                    <p>Pull request #41 merged after a successful build</p>
                    <button ejs-button id='commitBtn' cssClass='e-link' content='View commit'></button>
                    <button ejs-button #closeBtn id='closeBtn' cssClass='e-link' content='Dismiss' (click)="dismissClick()"></button>
                </ng-template>
            </ejs-message>
        </div>
    </div>`
})
export class AppComponent{
    @ViewChild('showBtn') private showBtn: ButtonComponent;
    @ViewChild('msg_template') private msgTemplate: MessageComponent;

    public showClick(): void {
        this.msgTemplate.visible = true;
        this.showBtn.element.classList.add('msg-hidden');
    }

    public dismissClick(): void {
        this.msgTemplate.visible = false;
    }

    public closed(): void {
        this.showBtn.element.classList.remove('msg-hidden');
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MessageModule } from '@syncfusion/ej2-angular-notifications';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, MessageModule, ButtonModule
    ],
    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);