Adding on NullԀʇɹ's answer, you can create a breadcrumb dynamically by using PrimeNG's breadcrumb.
You'll firstly need a BreadcrumbService:
import { EventEmitter, Injectable } from '@angular/core';
import { MenuModule, MenuItem } from 'primeng/primeng';
/**
* A service provides functions to get and set the breadcrumb of the app
*/
@Injectable()
export class BreadcrumbService {
items: MenuItem[] = [];
public breadcrumbChanged: EventEmitter<MenuItem[]> = new EventEmitter<MenuItem[]>();
getBreadcrumb(): MenuItem[] {
return this.items;
}
setBreadcrumb(breadcrumb: MenuItem[]): void {
this.items = breadcrumb;
this.breadcrumbChanged.next(this.items);
}
}
Then in your main component, say AppComponent, inject your BreadCrumbService, and tells it whenever breadcrumb changes, update it:
import { BreadcrumbService } from './breadcrumb.service';
import { BreadcrumbModule, MenuItem } from 'primeng/primeng';
@Component({
selector: 'my-app',
templateUrl: '/app.component.html',
})
export class AppComponent {
breadcrumb: MenuItem[];
constructor(private breadcrumbService: BreadcrumbService) {
// whenever breadcrumb changes, update it
this.breadcrumbService.breadcrumbChanged.subscribe(breadcrumb => { this.breadcrumb = breadcrumb });
}
}
And your corresponding html for your AppComponent should look like:
<p-breadcrumb [model]="breadcrumb"></p-breadcrumb>
Now in the component you want to dynamically create a breadcrumb for, say My-Product:
import { OnInit } from "@angular/core";
import { BreadcrumbService } from '../../common/breadcrumb.service';
import { MenuModule, MenuItem } from 'primeng/primeng';
@Component({
selector: 'my-product.component',
templateUrl: './my-product.component.html',
})
export class MyProduct implements OnInit {
breadcrumbItems: MenuItem[] = [];
constructor(private breadcrumb: BreadcrumbService) {}
ngOnInit() {
this.breadcrumbItems.push({label:'Products'});
this.breadcrumbItems.push({label:'Category-C', routerLink: ['/products/category-c']});
this.breadcrumbItems.push({label: 'My-Product'});
this.breadcrumb.setBreadcrumb(this.breadcrumbItems);
}
}