Open In App

Angular PrimeNG Breadcrumb Events

Last Updated : 02 Jan, 2023
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. In this article, we will learn about Breadcrumb Events in Angular PrimeNG.

The Breadcrumb component provides the navigation link that is used to link the previous pages the user navigates through & display it in the hierarchy. 

Angular PrimeNG Breadcrumb Events:

  • onItemClick: It is a callback that is fired when an item is selected. 

 

Syntax:

<p-breadcrumb 
    [model]="gfg">
    ...
</p-breadcrumb>

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 --save 

Example 1: Below is the example code that illustrates the use of Angular PrimeNG Breadcrumb Events. In this example, we will learn about onItemClick and display alerts when the breadcrumb is clicked.

  • app.component.html:

HTML




<h2 style="color: green;">GeeksforGeeks</h2>
<h3>Angular PrimeNG Breadcrumb Events</h3>
  
<p-breadcrumb [model]="gfg" 
    (onItemClick)="callAlert()">
</p-breadcrumb>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { MenuItem } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    gfg: MenuItem[] = [];
  
    ngOnInit() {
        this.gfg = [
            { label: "AngularJS" },
            { label: "ReactJS" },
            { label: "HTML" },
            { label: "JavaScript" },
            { label: "PrimeNG" }
        ];
    }
  
    callAlert() {
        alert("Hi Geek! Breadcrumb is clicked");
    }
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { BreadcrumbModule } from "primeng/breadcrumb";
  
@NgModule({
    imports: [
        BrowserModule, 
        BrowserAnimationsModule, 
        BreadcrumbModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG Breadcrumb Events. In this example, we will learn about onItemClick and add a breadcrumb with the name ‘I am added’, when the breadcrumb is clicked.

  • app.component.html:

HTML




<h2 style="color:green">GeeksforGeeks</h2>
<h3>Angular PrimeNG Breadcrumb Events</h3>
  
<p-breadcrumb [model]="gfg"
    (onItemClick)=addBreadcrumb()>
</p-breadcrumb>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { MenuItem } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    gfg: MenuItem[] = [];
  
    ngOnInit() {
        this.gfg = [
            { label: "AngularJS" },
            { label: "ReactJS" },
            { label: "HTML" },
            { label: "JavaScript" },
            { label: "PrimeNG" }
        ];
    }
  
    addBreadcrumb() {
        this.gfg.push({ label: "I am added" });
    }
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { BreadcrumbModule } from "primeng/breadcrumb";
  
@NgModule({
    imports: [
        BrowserModule, 
        BrowserAnimationsModule, 
        BreadcrumbModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Reference: https://primefaces.org/primeng/breadcrumb



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads