Open In App

Angular PrimeNG Properties of Messages Component

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 learn how to use the Messages Properties in Angular PrimeNG. We will also learn about the properties, along with their syntaxes that will be used in the code.

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



Angular PrimeNG Properties of Messages Component:

 



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: After complete installation, it will look like the following:

 

ng serve --save

Example 1: This is the basic example that shows how to use Angular PrimeNG Messages Properties.




<h1 style="color: green">GeeksforGeeks</h1>
<h5>PrimeNG Messages Properties</h5>
  
<p-messages [(value)]="gfg" 
            [enableService]="false">
</p-messages>




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




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

Output:

 

Example 2: This is another basic example that shows how to use Angular PrimeNG Messages Properties using dynamic messages.




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>Angular PrimeNG Messages Properties</h5>
    <button type="button" pButton pRipple
            (click)="showMssgs()"
            label="Show Messages"
            class="p-mr-3 p-button-success">
    </button>
  
    <button type="button" pButton pRipple
            (click)="clearMssgs()"
            label="Clear Messages"
            class="p-button-danger">
    </button>
  
    <p-messages [(value)]="gfg"
                 [enableService]="false">
    </p-messages>
</div>




import { Component } from "@angular/core";
import { Message } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
})
  
export class AppComponent {
    gfg: Message[];
  
    showMssgs() {
        this.gfg = [
            {
                severity: "success",
                summary: "Geek-Success",
                detail: "This is a success Geek",
            },
            {
                severity: "info",
                summary: "Geek-Info",
                detail: "This is a info Geek",
            },
        ];
    }
  
    clearMssgs() {
        this.gfg = [];
    }
}




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

Output:

 

Reference: https://primefaces.org/primeng/messages


Article Tags :