Customization in Angular Message component

1 Dec 20229 minutes to read

The Message component allows the user to customize the content display positions and appearance. This section explains the details about changing the content alignments and border styles for messages.

Content Alignment

Normally, the message content is aligned to the left. The Message component allows the user to align the message content in the center or right through the built-in classes e-content-center and e-content-right.

The following example demonstrates the message with different content alignments.

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

@Component({
    selector: 'app-root',
    template: `<div class="msg-custom-section">
        <div class="content-section">
            <h4>Content Alignment</h4>
            <ejs-message id="msg_content_left" content="Your license has been activated successfully" severity="Success"></ejs-message>
            <ejs-message id="msg_content_center" content="The license will expire today" cssClass="e-content-center" severity="Warning"></ejs-message>
            <ejs-message id="msg_content_right" content="The license key is invalid" cssClass="e-content-right" severity="Error"></ejs-message>
        </div>
    </div>`
})
export class AppComponent{
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MessageModule } from '@syncfusion/ej2-angular-notifications';
import { AppComponent } from './app.component';

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

Rounded and Square

To customize the Message component’s appearance, add the custom class to the message through the cssClass property. This custom class will be added to the root element. Based on this custom class, the user can override the message styles at the application level.

The following example shows the rounded and squared appearance of the message, which can be achieved by adding the cssClass property.

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

@Component({
    selector: 'app-root',
    template: `<div class="msg-custom-section">
        <div class="content-section">
            <h4>Rounded</h4>
            <ejs-message content="The license will expire today" cssClass="rounded" severity="Warning"></ejs-message>
            <h4>Square</h4>
            <ejs-message content="The license key is invalid" cssClass="square" severity="Error"></ejs-message>
        </div>
    </div>`
})
export class AppComponent{
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MessageModule } from '@syncfusion/ej2-angular-notifications';
import { AppComponent } from './app.component';

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

CSS Message

The Essential JS 2 Message has predefined CSS classes that can be defined in the HTML elements, which renders the message without any script reference. This can display a simple message with content and make the code lighter.

The following DOM structure is required to display the simple message with the content.

<div class="e-message">
    <div class="e-msg-content">..content..</div>
</div>

The following DOM structure is required to display the simple message with the content and severity icon.

<div class="e-message">
    <span class="e-msg-icon"></span>
    <div class="e-msg-content">..content..</div>
</div>

The following is the available list of predefined CSS classes to make the appearance of a message.

Class Description
e-message Represents the message wrapper.
e-msg-icon Represents the severity type icon.
e-msg-content Represents the message content.
e-msg-close-icon Represents the close icon.
e-info Represents the information message.
e-success Represents the success message.
e-warning Represents the warning message.
e-error Represents the error message.
e-content-center Aligns the message content to the center.
e-content-right Aligns the message content to the right.

The following example shows the message which renders without any script reference.

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

@Component({
    selector: 'app-root',
    template: `<div class="msg-default-section">
        <div class="content-section">
            <div id="msg-default" class="e-message" role="alert">
                <span class="e-msg-icon"></span>
                <div class="e-msg-content">Editing is restricted</div>
            </div>
            <div id="msg-info" class="e-message e-info" role="alert">
                <span class="e-msg-icon"></span>
                <div class="e-msg-content">Please read the comments carefully</div>
            </div>
            <div id="msg-success" class="e-message e-success" role="alert">
                <span class="e-msg-icon"></span>
                <div class="e-msg-content">Your message has been sent successfully</div>
            </div>
            <div id="msg-warning" class="e-message e-warning" role="alert">
                <span class="e-msg-icon"></span>
                <div class="e-msg-content">There was a problem with your network connection</div>
            </div>
            <div id="msg-error" class="e-message e-error" role="alert">
                <span class="e-msg-icon"></span>
                <div class="e-msg-content">A problem occurred while submitting your data</div>
            </div>
        </div>
    </div>`
})
export class AppComponent{
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MessageModule } from '@syncfusion/ej2-angular-notifications';
import { AppComponent } from './app.component';

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