Messages is used to display alerts inline.
import { MessagesModule } from 'primeng/messages';
A single message is specified by Message interface in PrimeNG that defines the id, severity, summary and detail as the properties. Messages to display can either be defined using the value property which should an array of Message instances or using a MessageService are defined using the value property which should an array of Message instances.
<p-messages [(value)]="messages" [enableService]="false" [closable]="false"></p-messages>
Messages are closable by default resulting in a close icon being displayed on top right corner. In order to disable closable messages, set closable to false. Note that in this case two-way binding is not necessary as the component does not need to update its value.
<p-messages [(value)]="messages1" [enableService]="false" [closable]="false"></p-messages>
<p-messages [(value)]="messages2" [enableService]="false"></p-messages>
A binding to the value property is required to provide messages to the component.
<button type="button" pButton pRipple (click)="addMessages()" label="Show" class="mr-2"></button>
<button type="button" pButton pRipple (click)="clearMessages()" icon="pi pi-times" label="Clear" class="p-button-secondary"></button>
<p-messages [(value)]="messages" [enableService]="false" [closable]="false"></p-messages>
MessageService alternative does not require a value binding to an array. In order to use this service, import the class and define it as a provider in a component higher up in the component tree such as application instance itself so that descandant components can have it injected. If there are multiple message components having the same message service, you may use key property of the component to match the key of the message to implement scoping.
<div class="flex justify-content-center gap-2">
<button type="button" pButton class="p-button-primary" (click)="addSingle()" label="Single"></button>
<button type="button" pButton class="p-button-success" (click)="addMultiple()" label="Multiple"></button>
<button type="button" pButton class="p-button-secondary" (click)="clear()" label="Clear"></button>
</div>
<p-messages></p-messages>
Alternative way to provide the content for the messages is templating. In this case value property and message service is ignored and only static is displayed.
<p-messages severity="info">
<ng-template pTemplate>
<img src="https://primefaces.org/cdn/primeng/images/primeng.svg" width="32" />
<div class="ml-2">Always bet on Prime.</div>
</ng-template>
</p-messages>
p-message component is used to display inline messages mostly within forms.
<div class="flex justify-content-center gap-2">
<p-message severity="info" text="Message Content"></p-message>
<p-message severity="success" text="Message Content"></p-message>
<p-message severity="warn" text="Message Content"></p-message>
<p-message severity="error" text="Message Content"></p-message>
</div>
<div class="mt-4">
<input type="text" pInputText placeholder="Username" class="ng-dirty ng-invalid mr-2">
<p-message severity="error" text="Field is required"></p-message>
</div>
<div class="mt-4">
<input type="text" pInputText placeholder="Email" class="ng-dirty ng-invalid mr-2">
<p-message severity="error"></p-message>
</div>
<div class="field p-fluid mt-4">
<label for="username">Username</label>
<input id="username" type="username" aria-describedby="username-help" class="ng-invalid ng-dirty" pInputText />
<small id="username-help" class="p-error">Username is not available.</small>
</div>
The severity option specifies the type of the message.
<p-messages [(value)]="messages" [enableService]="false" [closable]="false"></p-messages>
Transition of the open and hide animations can be customized using the showTransitionOptions and hideTransitionOptions properties, example below disables the animations altogether.
<p-messages [(value)]="messages" [showTransitionOptions]="'500ms'" [hideTransitionOptions]="'500ms'" [enableService]="false"></p-messages>
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-messages | Container element. |
p-message | Message element. |
p-message-info | Message element when displaying info messages. |
p-message-warn | Message element when displaying warning messages. |
p-message-error | Message element when displaying error messages. |
p-message-success | Message element when displaying success messages. |
p-message-close | Close button. |
p-message-close-icon | Close icon. |
p-message-icon | Severity icon. |
p-message-summary | Summary of a message. |
p-message-detail | Detail of a message. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-inline-message | Message element. |
p-inline-message-info | Message element when displaying info messages. |
p-inline-message-warn | Message element when displaying warning messages. |
p-inline-message-error | Message element when displaying error messages. |
p-inline-message-success | Message element when displaying success messages. |
p-inline-message-icon | Severity icon. |
p-inline-message-text | Text of a message. |
Message components use alert role that implicitly defines aria-live as "assertive" and aria-atomic as "true". Since any attribute is passed to the root element, attributes like aria-labelledby and aria-label can optionally be used as well.
Close element is a button with an aria-label that refers to the aria.close property of the locale API by default, you may usecloseButtonProps to customize the element and override the default aria-label.
Key | Function |
---|---|
enter | Closes the message. |
space | Closes the message. |
API defines helper props, events and others for the PrimeNG Messages module.
Name | Type | Default | Description |
---|---|---|---|
value | array | null | An array of messages to display. |
closable | boolean | true | Defines if message box can be closed by the click icon. |
style | string | null | Inline style of the component. |
styleClass | string | null | Style class of the component. |
enableService | boolean | true | Whether displaying services messages are enabled. |
escape | boolean | true | Whether displaying messages would be escaped or not. |
key | string | null | Id to match the key of the message to enable scoping in service based messaging. |
showTransitionOptions | string | 300ms ease-out | Transition options of the show animation. |
hideTransitionOptions | string | 200ms cubic-bezier(0.86, 0, 0.07, 1) | Transition options of the hide animation. |
Name | Type | Default | Description |
---|---|---|---|
severity | string | null | Severity level of the message. |
text | string | null | Text content. |
escape | boolean | true | Whether displaying messages would be escaped or not. |
style | string | null | Inline style of the component. |
styleClass | string | null | Style class of the component. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Parameters |
---|---|
content | - |