Open In App

Angular PrimeNG Messages Array

Last Updated : 08 Nov, 2022
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 see the Messages Array in Angular PrimeNG.

The Messages component is used to display a message with particular severity. To display messages user can make use of the value property. Message service is ignored and an array of messages is bound to value property, in order to share the message with the component.

Syntax:

<p-messages [(value)]="msgs"></p-messages>
<button type="button" 
        (click)="show()">Show
</button>
<button type="button" 
        (click)="clear()">Hide
</button>

 

Creating Angular application & Module Installation:

Step 1: To create an angular app, you need to install the angular command line interface through the npm command:

npm install -g angular-cli

Step 2: Now, we will create an angular project:

ng new project_name

Step 3: After creating your angular project, move into the folder to perform different operations:

cd project_name

Step 4: After creating the Angular application, Install the required module using the following command:

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

Project Structure: After successful installation, the following project structure will appear:

Project Structure

  • Step to Run Application: Run the application using the following command from the root directory of the project:
ng serve --open

Example 1: This example describes the Angular PrimeNG Messages Array.

  • app.component.html

HTML




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


  • app.module.ts

Javascript




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({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        MessageModule,
        MessagesModule,
        BrowserAnimationsModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }


  • app.component.ts

Javascript




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 = [
            { severity: 'info'
              summary: 'Info ',
              detail: 'Messages Array' },
            { severity: 'success'
              summary: 'Success '
              detail: 'Geeks for Geeks rocks' },
            { severity: 'warn'
              summary: 'Warning '
              detail: 'Alert Message' },
            { severity: 'error'
              summary: 'Error '
              detail: 'Report Bug!!' }
        ];
    }
}


Output:

 

Example 2: This example describes the Angular PrimeNG Messages Array.

  • app.component.html

HTML




<div style="margin:100px;">
    <h1 style="color: green">GeeksforGeeks</h1>
    <h3>Angular PrimeNG Messages Array</h3>
    <button pButton type="button" 
                    label="Show" 
                    class="p-button-success" 
                    (click)="show()">
    </button>
    <button pButton type="button" 
                    label="Hide" 
                    class="p-button-warning" 
                    (click)="hide()">
    </button>
    <p-messages [(value)]="gfg" 
                [enableService]="false" 
                [closable]="false">
    </p-messages>
</div>


  • app.module.ts

Javascript




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


  • app.component.ts

Javascript




import { Component } from '@angular/core';
import { Message } from "primeng/api";
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    gfg: Message[] = [];
    show() {
        this.gfg = [
            { severity: 'success'
              summary: 'Success '
              detail: 'Geeks for Geeks rocks' }
        ];
    }
    hide() {
        this.gfg = [];
    }
}


Output:

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads