Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(cdkMenu): menu template context is not updated when cdkMenuTriggerData is changed #26256

Open
1 task
ghostlytalamaur opened this issue Dec 13, 2022 · 6 comments
Labels
area: cdk/menu P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@ghostlytalamaur
Copy link

ghostlytalamaur commented Dec 13, 2022

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When cdkMenuTriggerData is used to pass additional context to a menu template the template context is not update when the data is changed.

Reproduction

GitHub
StackBlitz
Steps to reproduce:

  1. Open the application.
  2. Click the button "Click Me!". Menu with initial counter value is shown.
  3. Click "Increment" button.
  4. Click "Click Me!" button second time

Expected Behavior

Counter value in the context menu should be in sync with the value on the "Click Me!" button.

Actual Behavior

Counter value in the context menu keep initial value and is not updated when context changes.

Environment

  • Angular: 15.0.0
  • CDK/Material: 15.0.2
@ghostlytalamaur ghostlytalamaur added the needs triage This issue needs to be triaged by the team label Dec 13, 2022
@martinvillysson
Copy link

martinvillysson commented Mar 9, 2023

I'm also experiencing this issue. I think its because of this check:

if (this.menuTemplateRef && (!this._menuPortal || hasMenuContentChanged)) {

If i'm reading it correctly if the templateref hasnt changed the data is not updated.

@wagnermaciel
Copy link
Contributor

I believe the way you are using the cdkMenuTriggerData in the #menu template is wrong. Here is a working example:
https://stackblitz.com/edit/angular-s5r2am-ccdczn?file=src%2Fmain.ts

@ghostlytalamaur
Copy link
Author

ghostlytalamaur commented May 3, 2023

I believe the way you are using the cdkMenuTriggerData in the #menu template is wrong. Here is a working example:
https://stackblitz.com/edit/angular-s5r2am-ccdczn?file=src%2Fmain.ts

In your example you don't use template context at all. You use counter value from component class, not from template context.

So, why the issue was closed if the bug is still here?

@wagnermaciel
Copy link
Contributor

Oh whoops, that's my bad

@wagnermaciel wagnermaciel reopened this May 3, 2023
@wagnermaciel wagnermaciel added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent area: cdk/menu and removed needs triage This issue needs to be triaged by the team labels May 3, 2023
@heunetik
Copy link

Also stumbled upon this issue while trying to reorder a list with some cdkMenuTriggerData attached to the elements.
While definitely not perfect, re-rendering the trigger component does update the data bindings.

I've modified the initial StackBlitz to re-render on increment, and it seems to work just fine.

Thought this workaround might help someone while we get a proper way of handling this issue.

@IzioDev
Copy link

IzioDev commented Nov 7, 2023

We just encountered this behavior:

  • in a directive, if menuData is updated, the menu template using this context (menuData) is not re-rendered with new context values, which is very unexpected at first.

The solution @heunetik is offering does work, but feels hacky considering it should correctly detect that a re-render of the menu template is required (consumed data has changed)

Is Angular team planning to consider a fix for this or lead contributors to a desired fix?

Or shall we avoid this patterns? (that worked this way with MatMenu)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cdk/menu P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests

5 participants