question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NbContextMenu - not sure how to trigger methods on click of a menu item

See original GitHub issue

Issue type

I’m submitting a … (check one with “x”)

  • bug report
  • feature request

Issue description

Current behavior: At the moment, it’s unclear how to easily add actions to a click of a menu item using the nbContextMenu component.

There is some info in the documentation about adding an event listener and using pipe to get which item was clicked, however apart from creating a window alert showing the value that was clicked, I’m not sure how t0 trigger different methods depending on what was clicked.

Expected behavior: I would like to be able to create the menu array with two values instead of just providing a title. I’d like to be able to define a context menu like this:

userMenu = [
    { title: 'Profile', action: 'ProfileClicked()' },
    { title: 'Log out', action: 'logout()' }
]

Then passing this into the menu like so [nbContextMenu]=userMenu would trigger the methods I’ve defined in the array.

If this isn’t possible would really appreciate some help in how I could code this into the below example from the documentation:

ngOnInit() {
    this.nbMenuService.onItemClick()
      .pipe(
        filter(({ tag }) => tag === 'my-context-menu'),
        map(({ item: { title } }) => title),
      )
      .subscribe(title => this.window.alert(`${title} was clicked!`));
  }

Steps to reproduce: ngx-admin with the example above from the documentation implemented into the header.component.ts

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10

github_iconTop GitHub Comments

26reactions
ArslanAhmetcommented, Oct 23, 2019
this.menuService.onItemClick().subscribe((event) => {
  if (event.item.title === 'Log out') {
    console.log('logout clicked');
  }
});
9reactions
nnixaacommented, Oct 8, 2018

Hi @jjgriff93, currently there is no built-in way, but a menu item accepts an option called data, so that you can pass any info into it. So you can pass there som id and then check which item is clicked based on that id:

userMenu = [
    { title: 'Log out', data: { id: 'logout' } },
]
ngOnInit() {
    this.nbMenuService.onItemClick()
      .pipe(
        filter(({ tag }) => tag === 'my-context-menu'),
      )
      .subscribe((item: NbMenuItem) => {
		if (item.data.id === 'logout') { ... }
	  });
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Context Menu Angular UI Component - Nebular - GitHub Pages
Context menu has a number of triggers which provides an ability to show and hide the component in different ways: Click mode shows...
Read more >
Add Left and Right Click Event to MenuItem - Stack Overflow
I`m trying to add a event for left and right click on a MenuItem. Left click goes to an URL, right - call...
Read more >
Right click context menu with Angular | Marco.dev
Create a right click context menu inside a dynamic list or table ... matMenuTrigger: MatMenuTrigger; /** * Method called when the user click...
Read more >
Context Menu Angular UI Component - component api - Nebular
Available options: click , hover , hint , focus and noop. nbContextMenu. Basic menu items, will be passed to the internal NbMenuComponent.
Read more >
ngx-contextmenu - npm
Start using ngx-contextmenu in your project by running `npm i ... item will not emit execute events or close the context menu when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found