Open In App

Angular PrimeNG Toast Positions

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 Positions 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 
    position="..." 
    key="...">
</p-toast>

Angular PrimeNG Toast Positions properties:

  • severity: It is used to specify the severity level of the message. It is of string data type, the default value is null.
  • key: It is the Id to match the key of the message to enable scoping in service-based messaging. It is of string data type, the default value is null.
  • summary: It is used to define the summary text of the toast message. It is of string type and the default value is null.
  • detail: It is used to define the details text of the toast message. It is of string type and the default value is null.
  • label: It is used to give text value to the toast button component. It is a string type.
  • position: It is used to define the position of the toast. The acceptable positions for the component are top-right, top-left, bottom-left, bottom-right, top-center, bottom-center, and center. It is of string type and the default value is top-right.

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 Positions.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>Angular PrimeNG Toast Positions</h5>
  
    <button
      type="button"
      pButton
      pRipple
      (click)="TopLeftToast()"
      label="Top Left">
    </button>
  
    <button
      type="button"
      pButton
      pRipple
      (click)="TopCenterToast()"
      label="Top Center"
      class="p-button-warning">
    </button>
  
    <button
      type="button"
      pButton
      pRipple
      (click)="topRightToast()"
      label="Top Right"
      class="p-button-success">
    </button>
</div>
  
<p-toast 
      position="top-left" 
      key="topleft">
</p-toast>
<p-toast 
      position="top-center" 
      key="topcenter">
</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: 'info',
        summary: 'GeeksforGeeks',
        detail: 'Top left message',
      });
    }
  
    TopCenterToast() {
      this.messageService.add({
        key: 'topcenter',
        severity: 'warn',
        summary: 'GeeksforGeeks',
        detail: 'Top center message',
      });
    }
  
    topRightToast() {
      this.messageService.add({
        key: 'topright',
        severity: 'success',
        summary: 'GeeksforGeeks',
        detail: 'Top right 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 Positions.

app.component.html




<div style="text-align: center">
  <h2 style="color: green">GeeksforGeeks</h2>
  <h5>Angular PrimeNG Toast Positions</h5>
  
  <button
    type="button"
    pButton
    pRipple
    (click)="BottomLeftToast()"
    label="Bottom Left"
    class="p-button-danger">
  </button>
  
  <button
    type="button"
    pButton
    pRipple
    (click)="BottomCenterToast()"
    label="Bottom Center"
    class="p-button-primary">
  </button>
  
  <button
    type="button"
    pButton
    pRipple
    (click)="BottomRightToast()"
    label="Bottom Right"
    class="p-button-success">
  </button>
</div>
  
<p-toast 
    position="bottom-left" 
    key="bottomleft">
</p-toast>
<p-toast 
    position="bottom-center" 
    key="bottomcenter">
</p-toast>
<p-toast 
    position="bottom-right" 
    key="bottomright">
</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
    ) {}
  
    BottomLeftToast() {
      this.messageService.add({
        key: 'bottomleft',
        severity: 'error',
        summary: 'GeeksforGeeks',
        detail: 'Bottom left message',
      });
    }
  
    BottomCenterToast() {
      this.messageService.add({
        key: 'bottomcenter',
        severity: 'info',
        summary: 'GeeksforGeeks',
        detail: 'Bottom center message',
      });
    }
  
    BottomRightToast() {
      this.messageService.add({
        key: 'bottomright',
        severity: 'success',
        summary: 'GeeksforGeeks',
        detail: 'Bottom right 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