Open sherlock1982 opened 6 years ago
Short answer: Yes, you need the setTimeout.
Long answer: I haven't seen anyone try to do something like this, but here's what I'm guessing is happening.
Without the setTimeout:
[]
if you set it to that)<context-menu>
sees no contextMenuItem
sthis.contextMenuActions
does not immediately trigger change detection on the <context-menu>
so it uses the old values.With the setTimeout:
<context-menu>
component so that it picks up the new <ng-template contextMenuItem>
s.I have no good explanation for why the execute isn't fired.
Thank you for the explanation. What I was trying to achieve is this simple scenario:
I think it might make sense to update your example with Dynamic context menu
. Cause it actually demonstrates static context menu. It's important that after contextMenuActions are generated new change cycle should take place.
Yes, that's a valid use case and I agree that the <context-menu>
should handle it more elegantly, but it currently doesn't. Want to submit a PR to update the docs? I'm always looking for help and you can write clear explanations.
We're having the same issue with using dynamic context menus. When looking at the code we identified the issue being the ContentChildren Input inside the ContextMenuDirective. As you mentioned it's the changeDetection that didn't update the values between setting the items and opening the context menu.
We also tried to call the changedetection manually between the two lines of code but didn't work. Also calling setTimeout is not working properly. We have to use a specific time of milisecods to get it working.
this.contextMenuActions = actions;
this.triggerChange();
if (!actions || !actions.length || actions.every(a => a.hidden)) {
return;
}
setTimeout(() => {
this.contextMenuService.show.next({
event: event.event,
contextMenu: this.contextMenuComponent,
item: null
});
this.triggerChange();
}, 450);
In my opinion it's a very common use case to have one contextmenu and setting the entries dynamically with an ngFor. We're looking for a clean way to solve this, do you have any suggestions for us?
Currently I need to use dynamic context menu like this (approximately):
onContextMenu by itself is a click event somewhere on a button.
Though if I remove setTimeout two issues appear:
Is there a reason for setTimeout call or am I doing something wrong?