Angular PrimeNG Toast Template
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 Toast Template 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.
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:
- To run the above file, run the below command:
ng serve --save
Example 1: Below is the example code that illustrates the use of Angular PrimeNG Toast Template.
- app.component.html:
HTML
< p-toast position = "top-center" key = "gfg" (onClose)="reject()"> < ng-template let-message pTemplate = "message" > < div class = "p-flex p-flex-column" > < div class = "p-text-center" > < i class = "pi pi-exclamation-triangle" style = "font-size: 4rem" ></ i > < h4 >{{ message.summary}}</ h4 > < p >{{ message.detail}}</ p > </ div > < div class = "p-grid p-fluid" > < div class = "p-col-6" > < button type = "button" pButton (click)="confirm()" label = "✔" class = "p-button-success" > </ button > </ div > < div class = "p-col-6" > < button type = "button" pButton (click)="reject()" label = "✘" class = "p-button-danger" > </ button > </ div > </ div > </ div > </ ng-template > </ p-toast > < h1 style = "color: green" >w3wiki</ h1 > < h5 >Angular PrimeNG Toast Template</ h5 > < button type = "button" pButton pRipple (click)="ConfirmBox()" label = "w3wiki" icon = "pi pi-code" > </ button > |
- app.component.ts:
Javascript
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, ) { } ConfirmBox() { this .messageService.add({ key: 'gfg' , sticky: true , severity: 'info' , summary: 'Hey Geek, are you sure?' , }); } confirm() { this .messageService.clear( 'gfg' ); } reject() { this .messageService.clear( 'gfg' ); } clear() { this .messageService.clear(); } } |
- app.module.ts:
Javascript
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: Below is another example code that illustrates the use of Angular PrimeNG Toast Template.
- app.component.html:
HTML
< p-toast position = "top-center" key = "gfg" (onClose)="reject()"> < ng-template let-message pTemplate = "message" > < div class = "p-flex p-flex-column" > < div class = "p-text-center" > < i class = "pi pi-exclamation-triangle" style = "font-size: 4rem" > </ i > < h4 >{{ message.summary}}</ h4 > < p >{{ message.detail}}</ p > </ div > < div class = "p-grid p-fluid" > < div class = "p-col-6" > < button type = "button" pButton (click)="confirm()" label = "✔" class = "p-button-success" > </ button > </ div > < div class = "p-col-6" > < button type = "button" pButton (click)="reject()" label = "✘" class = "p-button-danger" > </ button > </ div > </ div > </ div > </ ng-template > </ p-toast > < h1 style = "color: green" >w3wiki</ h1 > < h5 >Angular PrimeNG Toast Template</ h5 > < button type = "button" pButton pRipple (click)="ConfirmBox()" label = "w3wiki" icon = "pi pi-code" > </ button > |
- app.component.ts:
Javascript
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, ) { } ConfirmBox() { this .messageService.addAll([ { key: 'gfg' , severity: 'success' , summary: 'Hey Geek, are you sure?' }, { key: 'gfg' , severity: 'info' , summary: 'Hey Geek, are you sure?' }, ]); } confirm() { this .messageService.clear( 'gfg' ); } reject() { this .messageService.clear( 'gfg' ); } clear() { this .messageService.clear(); } } |
- app.module.ts:
Javascript
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: https://primefaces.org/primeng/toast