Open In App

Angular PrimeNG Toast Clear

Last Updated : 05 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 Clear 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 Clear properties:

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

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

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>Angular PrimeNG Toast Clear</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>
  
    <button type="button" pButton pRipple 
        (click)="clearToast()" 
        icon="pi pi-times" 
        class="p-button-danger"
        label="Clear Geek">
    </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',
        });
    }
  
    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:

 

Example 2: Below is another example that illustrates the use of the Angular PrimeNG Toast Clear using the toast positions.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>Angular PrimeNG Toast Clear</h5>
    <button type="button" pButton pRipple 
        (click)="TopLeftToast()" 
        label="Success Geek" 
        class="p-button-success">
    </button>
  
    <button type="button" pButton pRipple 
        (click)="TopRightToast()" 
        label="Info Geek" 
        class="p-button-info">
    </button>
  
    <button type="button" pButton pRipple 
        (click)="clearToast()" 
        icon="pi pi-times" 
        class="p-button-danger"
        label="Clear Geek">
    </button>
</div>
  
<p-toast position="top-left" key="topleft">
</p-toast>
<p-toast position="top-right" key="topright">
</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
    ) { }
  
    TopLeftToast() {
        this.messageService.add({
            key: 'topleft',
            severity: 'success',
            summary: 'GeeksforGeeks',
            detail: 'Message Content',
        });
    }
  
    TopRightToast() {
        this.messageService.add({
            key: 'topright',
            severity: 'info',
            summary: 'GeeksforGeeks',
            detail: 'Message Content',
        });
    }
  
    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