MessagesMessages is used to display messages inline.

Messages - 图1

Documentation

Import

  1. import {MessagesModule} from 'primeng/messages';
  2. import {MessageModule} from 'primeng/message';
  3.  

Getting Started

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.

  1. <p-messages [(value)]="msgs"></p-messages>
  2.  

Message Array

A binding to the value property is required to provide messages to the component.

  1. <p-messages [(value)]="msgs"></p-messages>
  2. <button type="button" (click)="show()">Show</button>
  3. <button type="button" (click)="clear()">Hide</button>
  4.  
  1. show() {
  2. this.msgs.push({severity:'info', summary:'Info Message', detail:'PrimeNG rocks'});
  3. }
  4. hide() {
  5. this.msgs = [];
  6. }
  7.  

Message Service

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.

  1. import {MessageService} from 'primeng/api';
  2.  
  1. import {Component} from '@angular/core';
  2. import {Message} from 'primeng//api';
  3. import {MessageService} from 'primeng/api';
  4. @Component({
  5. templateUrl: './messagesdemo.html'
  6. })
  7. export class MessageServiceDemo {
  8. constructor(private messageService: MessageService) {}
  9. addSingle() {
  10. this.messageService.add({severity:'success', summary:'Service Message', detail:'Via MessageService'});
  11. }
  12. addMultiple() {
  13. this.messageService.addAll([{severity:'success', summary:'Service Message', detail:'Via MessageService'},
  14. {severity:'info', summary:'Info Message', detail:'Via MessageService'}]);
  15. }
  16. clear() {
  17. this.messageService.clear();
  18. }
  19. }
  20.  

Closable

Messages are closable by default resulting in a close icon being displayed on top right corner.

  1. <p-messages [(value)]="msgs"></p-messages>
  2.  

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.

  1. <p-messages [value]="msgs" [closable]="false"></p-messages>
  2.  

Severities

Here are the possible values for the severity of a message.

  • success
  • info
  • warn
  • error

Message Component

Message component is useful in cases where messages need to be displayed related to an element such as forms. It has two property, severity and text of the message.

  1. <h3>Inline Message CSS</h3>
  2. <p>CSS helpers to display inline messages mostly within forms.</p>
  3. <p-message severity="info" text="PrimeNG Rocks"></p-message>
  4. <p-message severity="success" text="Record Saved"></p-message>
  5. <p-message severity="warn" text="Are you sure?"></p-message>
  6. <p-message severity="error" text="Field is required"></p-message>
  7.  

Animation Configuration

Transition of the open and hide animations can be customized using the showTransitionOptions and hideTransitionOptions properties, example below disables the animations altogether.

  1. <p-message [showTransitionOptions]="'0ms'" [hideTransitionOptions]="'0ms'" severity="info" text="PrimeNG Rocks"></p-message>
  2.  

Properties

NameTypeDefaultDescription
valuearraynullAn array of messages to display.
closablebooleantrueDefines if message box can be closed by the click icon.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
enableServicebooleantrueWhether displaying services messages are enabled.
keystringnullId to match the key of the message to enable scoping in service based messaging.
showTransitionOptionsstring300ms ease-outTransition options of the show animation.
hideTransitionOptionsstring250ms ease-inTransition options of the hide animation.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-messagesContainer element.
ui-messages-infoContainer element when displaying info messages.
ui-messages-warnContainer element when displaying warning messages.
ui-messages-errorContainer element when displaying error messages.
ui-messages-successContainer element when displaying success messages.
ui-messages-closeClose icon.
ui-messages-iconSeverity icon.
ui-messages-summarySummary of a message.
ui-messages-detailDetail of a message.

Dependencies

None.

Source

View on GitHub

  1. <p-messages [(value)]="msgs"></p-messages>
  2. <h3 class="first">Basic</h3>
  3. <div>
  4. <button type="button" pButton (click)="showSuccess()" label="Success" class="ui-button-success"></button>
  5. <button type="button" pButton (click)="showInfo()" label="Info" class="ui-button-info"></button>
  6. <button type="button" pButton (click)="showWarn()" label="Warn" class="ui-button-warning"></button>
  7. <button type="button" pButton (click)="showError()" label="Error" class="ui-button-danger"></button>
  8. <button type="button" pButton (click)="showMultiple()" label="Multiple"></button>
  9. <button type="button" pButton (click)="clear()" icon="pi pi-times" style="float:right" label="Clear"></button>
  10. </div>
  11. <h3>Message Service</h3>
  12. <button type="button" pButton (click)="showViaService()" label="Use Service"></button>
  13. <h3>Inline Message CSS</h3>
  14. <p>CSS helpers to display inline messages mostly within forms.</p>
  15. <p-message severity="info" text="PrimeNG Rocks"></p-message>
  16. <p-message severity="success" text="Record Saved"></p-message>
  17. <p-message severity="warn" text="Are you sure?"></p-message>
  18. <p-message severity="error" text="Field is required"></p-message>
  19. <div style="margin-top:30px">
  20. <input type="text" pInputText placeholder="Username" class="ng-dirty ng-invalid">
  21. <p-message severity="error" text="Field is required"></p-message>
  22. </div>
  23. <div style="margin-top:30px">
  24. <input type="text" pInputText placeholder="Email" class="ng-dirty ng-invalid">
  25. <p-message severity="error"></p-message>
  26. </div>
  27.  
  1. import {Component} from '@angular/core';
  2. import {SelectItem} from 'primeng/components/common/api';
  3. import {Message} from 'primeng/components/common/api';
  4. import {MessageService} from 'primeng/components/common/messageservice';
  5. @Component({
  6. templateUrl: './messagesdemo.html',
  7. styles: [`
  8. :host ::ng-deep button {
  9. margin-right: .25em;
  10. }
  11. :host ::ng-deep .ui-message,
  12. :host ::ng-deep .ui-inputtext {
  13. margin-right: .25em;
  14. }
  15. `],
  16. providers: [MessageService]
  17. })
  18. export class MessagesDemo {
  19. msgs: Message[] = [];
  20. constructor(private messageService: MessageService) {}
  21. showSuccess() {
  22. this.msgs = [];
  23. this.msgs.push({severity:'success', summary:'Success Message', detail:'Order submitted'});
  24. }
  25. showInfo() {
  26. this.msgs = [];
  27. this.msgs.push({severity:'info', summary:'Info Message', detail:'PrimeNG rocks'});
  28. }
  29. showWarn() {
  30. this.msgs = [];
  31. this.msgs.push({severity:'warn', summary:'Warn Message', detail:'There are unsaved changes'});
  32. }
  33. showError() {
  34. this.msgs = [];
  35. this.msgs.push({severity:'error', summary:'Error Message', detail:'Validation failed'});
  36. }
  37. showMultiple() {
  38. this.msgs = [];
  39. this.msgs.push({severity:'info', summary:'Message 1', detail:'PrimeNG rocks'});
  40. this.msgs.push({severity:'info', summary:'Message 2', detail:'PrimeUI rocks'});
  41. this.msgs.push({severity:'info', summary:'Message 3', detail:'PrimeFaces rocks'});
  42. }
  43. showViaService() {
  44. this.messageService.add({severity:'success', summary:'Service Message', detail:'Via MessageService'});
  45. }
  46. clear() {
  47. this.msgs = [];
  48. }
  49. }
  50.