MenuModel APIPrimeNG menus components share a common api to specify the menuitems and submenus.

MenuItem

Core of the api is MenuItem class that defines various options such as the label, icon and children of an item in a menu. Example below is a sample configuration with Menu component.

  1. <p-menu [model]="items"></p-menu>
  2.  
  1. import {MenuModule} from 'primeng/menu';
  2. import {MenuItem} from 'primeng/api';
  3.  
  1. export class MenuDemo {
  2. private items: MenuItem[];
  3. ngOnInit() {
  4. this.items = [{
  5. label: 'File',
  6. items: [
  7. {label: 'New', icon: 'fa fa-plus'},
  8. {label: 'Open', icon: 'fa fa-download'}
  9. ]
  10. },
  11. {
  12. label: 'Edit',
  13. items: [
  14. {label: 'Undo', icon: 'fa fa-refresh'},
  15. {label: 'Redo', icon: 'fa fa-repeat'}
  16. ]
  17. }];
  18. }
  19. }
  20.  

MenuItem provides the following properties. Note that not all of them may be utilized by the menu component.

NameTypeDefaultDescription
idstringnullIdentifier of the element.
labelstringnullText of the item.
iconstringnullIcon of the item.
commandfunctionnullCallback to execute when item is clicked.
urlstringnullExternal link to navigate when item is clicked.
routerLinkarraynullRouterLink definition for internal navigation.
routerLinkActiveOptionsobjectnullConfiguration for active router link.
queryParamsobjectnullQuery parameters for internal navigation via routerLink.
itemsarraynullAn array of children menuitems.
expandedbooleanfalseVisibility of submenu.
disabledbooleanfalseWhen set as true, disables the menuitem.
visiblebooleantrueWhether the dom element of menuitem is created or not.
targetstringnullSpecifies where to open the linked document.
separatorbooleanfalseDefines the item as a separator.
styleobjectnullInline style of the menuitem.
styleClassstringnullStyle class of the menuitem.
badgestringnullValue of the badge.
badgeStyleClassstringnullStyle class of the badge.
titlestringnullTooltip text of the item.
automationIdanynullValue of HTML data-* attribute.

Command

The function to invoke when an item is clicked is defined using the command property.

  1. export class MenuDemo {
  2. private items: MenuItem[];
  3. ngOnInit() {
  4. this.items = [{
  5. label: 'File',
  6. items: [
  7. {label: 'New', icon: 'fa fa-plus', command: (event) => {
  8. //event.originalEvent: Browser event
  9. //event.item: menuitem metadata
  10. }}
  11. ]
  12. }
  13. }
  14. }
  15.  

Navigation

Navigation is specified using url property for external links and with routerLink for internal ones. If a menuitem has an active route, ui-state-active style class is added as an indicator. Active route link can be configured with routerLinkActiveOptions property of MenuItem API.

  1. export class MenuDemo {
  2. private items: MenuItem[];
  3. ngOnInit() {
  4. this.items = [{
  5. label: 'File',
  6. items: [
  7. {label: 'New', icon: 'fa fa-plus', url: 'http://www.primefaces.org/primeng'},
  8. {label: 'Open', icon: 'fa fa-download', routerLink: ['/pagename']}
  9. {label: 'Recent Files', icon: 'fa fa-download', routerLink: ['/pagename'], queryParams: {'recent': 'true'}}
  10. ]
  11. }
  12. }
  13. }
  14.