Open In App

Angular PrimeNG Toast 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. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we will see the Angular PrimeNG Toast Component.

The Toast Component can be utilized to render the information or any message in an overlay. There are different kinds of toasts provided by the Angular PrimeNG, that can utilized to display the multiple message at once or stick to the specific position with having the different level of severities of messages & can be aligned them in various positions. The Toast Component also provide the specific template, in order to display on confirmation that can be cleared by clicking the button.



Angular PrimeNG Toast Components:

Syntax: Create a Toast as follows:



import {ToastModule} from 'primeng/toast';
<p-toast>...</p-toast>
this.messageService.add({severity:'success', 
                         summary:'New Message', 
                         detail:'GeeksforGeeks'});

Creating Angular application & Module Installation:

ng new geeks_angular
cd geeks_angular
npm install primeng --save
npm install primeicons --save

Project Structure: The project structure will look like the following:

 

Example 1: In the following example, we have different toast components in different positions.




<h1 style="color: green;
           text-align:center;">
      GeeksforGeeks
</h1>
<h3>Angular PrimeNG Toast Component</h3>
<p-toast></p-toast>
<p-toast position="top-left"
         key="tl">
</p-toast>
<p-toast position="top-center"
         key="tc">
</p-toast>
<p-toast position="bottom-center"
         key="bc">
</p-toast>
 
<h5>Positions</h5>
<div class="card" style="display:flex;
                         justify-content:space-evenly">
    <button type="button" pButton pRipple
            (click)="showTopLeft()"
            label="Top Left">
    </button>
    <button type="button" pButton pRipple
            (click)="showTopCenter()"
            label="Top Center"
        class="p-button-warning">
    </button>
    <button type="button" pButton pRipple
            (click)="showBottomCenter()"
            label="Bottom Center"
            class="p-button-success">
    </button>
</div>




import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
 
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [MessageService],
})
export class AppComponent {
    constructor(
        private messageService: MessageService,
        private primengConfig: PrimeNGConfig
    ) { }
 
    ngOnInit() {
        this.primengConfig.ripple = true;
    }
 
    showTopLeft() {
        this.messageService.add({
            key: 'tl',
            severity: 'info',
            summary: 'Info',
            detail: 'Message Content',
        });
    }
 
    showTopCenter() {
        this.messageService.add({
            key: 'tc',
            severity: 'success',
            summary: 'Info',
            detail: 'Message Content',
        });
    }
 
    showBottomCenter() {
        this.messageService.add({
            key: 'bc',
            severity: 'warn',
            summary: 'Info',
            detail: 'Message Content',
        });
    }
}




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 { ButtonModule } from 'primeng/button';
import { ToastModule } from 'primeng/toast';
import { RippleModule } from 'primeng/ripple';
 
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ToastModule,
        ButtonModule,
        RippleModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

Output:

 

Example 2: In the following example, we have Toast with different levels of severities of messages.




<h1 style="color: green;
           text-align:center;">
      GeeksforGeeks
</h1>
<h3>Angular PrimeNG Toast Component</h3>
<p-toast></p-toast>
<p-toast position="top-left"
         key="tl">
</p-toast>
<p-toast position="top-center"
         key="tc">
</p-toast>
<p-toast position="bottom-center"
         key="bc">
</p-toast>
 
<h5>Severities</h5>
<div class="card"
     style="display:flex;
            justify-content:space-evenly">
    <button type="button" pButton pRipple
            (click)="showSuccess()"
            label="Success"
            class="p-button-success">
    </button>
    <button type="button" pButton pRipple
            (click)="showInfo()"
            label="Info"
            class="p-button-info">
    </button>
    <button type="button" pButton pRipple
            (click)="showWarn()"
            label="Warn"
            class="p-button-warning">
    </button>
    <button type="button" pButton pRipple
            (click)="showError()"
            label="Error"
            class="p-button-danger">
    </button>
</div>




import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
 
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [MessageService],
})
export class AppComponent {
    constructor(
        private messageService: MessageService,
        private primengConfig: PrimeNGConfig
    ) { }
 
    ngOnInit() {
        this.primengConfig.ripple = true;
    }
 
    showSuccess() {
        this.messageService.add({
            severity: 'success',
            summary: 'Success',
            detail: 'Welcome to GeeksforGeeks',
        });
    }
 
    showInfo() {
        this.messageService.add({
            severity: 'info',
            summary: 'Info',
            detail: 'Welcome to GeeksforGeeks',
        });
    }
 
    showWarn() {
        this.messageService.add({
            severity: 'warn',
            summary: 'Warn',
            detail: 'Welcome to GeeksforGeeks',
        });
    }
 
    showError() {
        this.messageService.add({
            severity: 'error',
            summary: 'Error',
            detail: 'Welcome to GeeksforGeeks',
        });
    }
}




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 { ButtonModule } from 'primeng/button';
import { ToastModule } from 'primeng/toast';
import { RippleModule } from 'primeng/ripple';
 
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ToastModule,
        ButtonModule,
        RippleModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

Output:

 

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


Article Tags :