Open In App

Angular PrimeNG Toast Severities

Last Updated : 29 Aug, 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. This article will show us how to use the Toast Severities in Angular PrimeNG. We will also learn about the properties along with their syntaxes that will be used in the code.

The Toast component is a tool used to overlay feedback messages with particular severity.

Syntax:

<button
    type="button"
    pButton
    pRipple
    (click)="..."
    label="..."
    class="...">
</button>

<p-toast></p-toast>

Angular PrimeNG Toast Severities properties:

  • label: It is used to give text value to the toast button component. It is a string type.

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:

 

Run the below command to see the output:

ng serve --open

Example 1: Below is the example code that illustrates the use of Angular PrimeNG Toast Severities.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>Angular PrimeNG Toast Severities</h5>
    <button
        type="button"
        pButton
        pRipple
        (click)="SuccessToast()"
        label="Success Geek"
        class="p-button-success">
    </button>
  
    <button
        type="button"
        pButton
        pRipple
        (click)="InfoToast()"
        label="Info Geek"
        class="p-button-info">
    </button>
  </div>
  
<p-toast></p-toast>


app.component.ts




import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
    providers: [MessageService],
})
  
export class AppComponent {
    constructor(
      private messageService: MessageService
    ) {}
  
    SuccessToast() {
      this.messageService.add({
        severity: 'success',
        summary: 'GeeksforGeeks',
        detail: 'Success Message',
      });
    }
  
    InfoToast() {
      this.messageService.add({
        severity: 'info',
        summary: 'GeeksforGeeks',
        detail: 'Info 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 { ButtonModule } from 'primeng/button';
import { ToastModule } from 'primeng/toast';
  
@NgModule({
    imports: [
      BrowserModule,
      BrowserAnimationsModule,
      ToastModule,
      ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule {}


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG Toast Severities.

app.component.html




<div style="text-align: center">
  <h2 style="color: green">GeeksforGeeks</h2>
  <h5>Angular PrimeNG Toast Severities</h5>
  
  <button
    type="button"
    pButton
    pRipple
    (click)="WarnToast()"
    label="Warn Geek"
    class="p-button-warning">
  </button>
  
  <button
    type="button"
    pButton
    pRipple
    (click)="ErrorToast()"
    label="Error Geek"
    class="p-button-danger">
  </button>
</div>
<p-toast></p-toast>


app.component.ts




import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
    providers: [MessageService],
})
  
export class AppComponent {
    constructor(
      private messageService: MessageService
    ) {}
  
    WarnToast() {
      this.messageService.add({
        severity: 'warn',
        summary: 'GeeksforGeeks',
        detail: 'Warn Message',
      });
    }
  
    ErrorToast() {
      this.messageService.add({
        severity: 'error',
        summary: 'GeeksforGeeks',
        detail: 'Error 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 { ButtonModule } from 'primeng/button';
import { ToastModule } from 'primeng/toast';
  
@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    ToastModule,
    ButtonModule
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
  
export class AppModule {}


Output:

 

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



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

Similar Reads