Open In App

Angular PrimeNG Messages Component

Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the Messages Component in Angular PrimeNG. We will also learn about the properties, styling along with their syntaxes that will be used in the code. 

Messages component: It is used to display a message with particular severity.

Properties of Messages Component:

  • value: It is an array of messages to display. It is of array data type, the default value is null.
  • closable: It defines if the message box can be closed by the click icon. It is of the boolean data type, the default value is true.
  • style: It sets the inline style of the component. It is of string data type, the default value is null.
  • styleClass: It sets the style class of the component. It is of string data type, the default value is null.
  • enableService: It specifies whether displaying services messages are enabled. It is of the boolean data type, the default value is true.
  • escape: It specifies whether displaying messages would be escaped or not. It is of boolean data type, the default value is true.
  • key: It is the Id to match the key of the message to enable scoping in service-based messaging. It is of string data type, the default value is null.
  • showTransitionOptions: It sets the transition options of the show animation. It is of the boolean data type, the default value is 300ms ease-out.
  • hideTransitionOptions: It sets the transition options of the hide animation. It is of the boolean data type, the default value is 200ms cubic-bezier(0.86, 0, 0.07, 1).

Styling for Messages Component:

  • p-messages: It is a container element.
  • p-message: It is a message element.
  • p-message-info: It is a message element when displaying info messages.
  • p-message-warn: It is a message element when displaying warning messages.
  • p-message-error: It is a message element when displaying error messages.
  • p-message-success: It is a message element when displaying success messages.
  • p-message-close: It is a close button.
  • p-message-close-icon: It is a close icon.
  • p-message-icon: It is a severity icon.
  • p-message-summary: It is a summary of a message.
  • p-message-detail: It is a detail of a message.

 

Properties of Message Component:

  • severity: It is used to specifies the severity level of the message. It is of string data type, the default value is null.
  • text: It is used to set the text content. It is of string data type, the default value is null.
  • escape: Whether displaying messages would be escaped or not. boolean true
  • style: It is used to set the inline style of the component. It is of string data type, the default value is null.
  • styleClass: It is used to sets the style class of the component. It is of string data type, the default value is null.

Styling for Message Component:

  • p-inline-message: It is a message element.
  • p-inline-message-info: It is a message element when displaying info messages.
  • p-inline-message-warn: It is a message element when displaying warning messages.
  • p-inline-message-error: It is a message element when displaying error messages.
  • p-inline-message-success: It is a message element when displaying success messages.
  • p-inline-message-icon: It is used to specify the severity icon.
  • p-inline-message-text: It is a text message.

Creating Angular application & module installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

Example 1: This is the basic example that illustrates how to use the Messages component.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Messages Component</h5>
<p-messages [(value)]="gfg" 
  [enableService]="false">
</p-messages>


app.component.ts




import { Component } from "@angular/core";
import { Message } from "primeng/api";
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  gfg: Message[];
  ngOnInit() {
    this.gfg = [
      { severity: 'success'
        summary: 'GeeksforGeeks'
        detail: "This is a message" 
      },
      { severity: 'info'
        summary: 'GeeksforGeeks'
        detail: "This is a message" 
      },
      { severity: 'warn'
        summary: 'GeeksforGeeks'
        detail: "This is a message" 
      },
      { severity: 'error'
        summary: 'GeeksforGeeks'
        detail: "This is a message" 
      }
    ];
  }
}


app.module.ts




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
  
import { AppComponent } from "./app.component";
import { MessagesModule } from "primeng/messages";
import { MessageModule } from "primeng/message";
  
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MessagesModule,
    MessageModule,
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}


Output:

Example 2: In this example, we have cleared the messages using the button.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Messages Component</h5>
<p-messages [(value)]="msgs"></p-messages>
<button type="button" (click)="hide()">Hide</button>


app.component.ts




import { Component } from "@angular/core";
import { Message } from "primeng/api";
  
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  msgs = [
    {
      severity: "success",
      summary: "GeeksforGeeks",
      detail: "This is a message",
    },
  ];
  hide() {
    this.msgs = [];
  }
  ngOnInit() {}
}


app.module.ts




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
  
import { AppComponent } from "./app.component";
import { MessagesModule } from "primeng/messages";
import { MessageModule } from "primeng/message";
  
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MessagesModule,
    MessageModule,
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}


Output:

Reference: https://primeng.org/messages



Last Updated : 17 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads