Open In App

Angular PrimeNG Toast Multiple

Last Updated : 08 Sep, 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 Toast Multiple 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)="..."
   class="..."
   label="...">
</button>

<p-toast></p-toast>

Angular PrimeNG Toast Multiple 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:

 

Steps to run the application: Run the below command to see the output:

ng serve --open

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

app.component.html




<p-toast></p-toast>
  
<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>Angular PrimeNG Toast Multiple</h5>
    <button type="button" pButton pRipple 
            (click)="MultipleToast()" 
            label="Multiple Geeks" 
            class="p-button-warning">
    </button>
</div>


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
    ) { }
  
    MultipleToast() {
        this.messageService.addAll([
            {
                severity: 'success',
                summary: 'Geek 1',
                detail: 'GeeksforGeeks',
            },
            {
                severity: 'error',
                summary: 'Geek 2',
                detail: 'It is a Computer 
                   Science portal',
        },
            {
                severity: 'warn',
                summary: 'Geek 3',
                detail: 'For all the geeks',
            },
        ]);
    }
}


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 that illustrates the use of the Angular PrimeNG Toast Multiple using the sticky toast.

app.component.html




<p-toast></p-toast>
  
<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>Angular PrimeNG Toast Multiple</h5>
    <button type="button" pButton pRipple 
            (click)="StickyToast()" 
            label="Sticky Geeks" 
            class="p-button-success">
    </button>
  
    <button type="button" pButton pRipple 
            (click)="clearToast()" 
            icon="pi pi-times" 
            label="Clear Geeks"
        class="p-button-danger">
    </button>
</div>


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
    ) { }
  
    StickyToast() {
        this.messageService.add({
            severity: 'success',
            summary: 'GeeksforGeeks',
            detail: 'It is a CS Portal.',
            sticky: true,
        });
    }
  
    clearToast() {
        this.messageService.clear();
    }
}


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