In an Angular CDK menu bar, why does a `cdkMenuItemCheckbox` for 'Show ruler' come back unchecked each time its View menu reopens, and how do you fix it?
answer
- the menu is a template
- destroyed on close, created on open
- checkbox items hold no lasting state
- an input aliased cdkMenuItemChecked
- listen to cdkMenuItemTriggered
basics
~20 sA CDK pop-up menu is an ng-template rendered into an overlay on open and destroyed on close, so a checkbox item's internal toggle is lost. Keep the state in the component: bind cdkMenuItemChecked and update it from cdkMenuItemTriggered.
solid answer
~40 s`cdkMenuTriggerFor` takes a reference to an `ng-template` holding the `cdkMenu`. Each time the menu opens, the trigger attaches that template to an overlay as a fresh embedded view, and closing detaches and destroys it. A `cdkMenuItemCheckbox` flips its own `checked` field when triggered, so it looks right until the menu closes; the next open builds a new item whose `checked` is the input's default, `false`. The CDK menu guide says it outright: checkbox and radio items do not track their own state. Bind `[cdkMenuItemChecked]="showRuler()"` and toggle the signal in `(cdkMenuItemTriggered)`. The item still sets `role="menuitemcheckbox"` and `aria-checked` from that value, and it needs no extra `cdkMenuItem` directive.
go deeper
Remember that a CDK pop-up menu is an ng-template opened by cdkMenuTriggerFor, and that its checkbox items need their checked state bound.
Explain the lifecycle: the trigger attaches the template as a new view on open and destroys it on close, so state must live in the component.
Treat every menu item as a stateless view of component state, using cdkMenuItemChecked, cdkMenuItemTriggered and cdkMenuGroup, and check for the 22.0.0 MenuTracker rename.
Set the kit rule that menu state is owned by the application, which also keeps toolbar buttons, shortcuts and menus in agreement.
## The setup A document editor's in-house kit builds its menu bar on the Angular CDK menu package (`@angular/cdk/menu`). The View menu has a **Show ruler** checkbox item. Users tick it, the menu closes, and the next time View opens the box is empty, even though the ruler is visible. ## How CDK pop-up menus live and die The CDK menu has two kinds of menus: - **Inline menus**: a `cdkMenu` or `cdkMenuBar` placed directly on the page. They exist as long as the page does. - **Pop-up menus**: a `cdkMenu` wrapped in an `ng-template` and referenced by a trigger's `cdkMenuTriggerFor` (or `cdkContextMenuTriggerFor` for right-click menus). For a pop-up menu, the trigger: 1. wraps the template in a `TemplatePortal`; 2. on open, creates (or reuses) an overlay and attaches the portal, which **creates a new embedded view**: new DOM, new directive instances; 3. on close, detaches the portal, which **removes and destroys that view**. Every directive inside the menu, including each menu item, is therefore re-created on every open. ## Why the checkbox forgets `cdkMenuItemCheckbox` extends a selectable base class that adds one input, `checked`, exposed under the alias **`cdkMenuItemChecked`**, defaulting to `false`. When the user triggers the item, it emits `cdkMenuItemTriggered` and then flips its own `checked` field. That local flip is why the box looks ticked while the menu is still open. The flip is never written back anywhere, and the instance holding it is destroyed with the view. The next open builds a fresh item whose `checked` comes from the input, and nothing bound it. The CDK menu guide states the rule: checkbox items **do not track their own state**; bind `cdkMenuItemChecked` and listen to `cdkMenuItemTriggered`, or the state resets when the menu is closed and reopened. The same applies to `cdkMenuItemRadio`. ## The fix ```html <div cdkMenuBar> <button cdkMenuItem [cdkMenuTriggerFor]="viewMenu">View</button> </div> <ng-template #viewMenu> <div cdkMenu> <button cdkMenuItemCheckbox [cdkMenuItemChecked]="showRuler()" (cdkMenuItemTriggered)="showRuler.set(!showRuler())"> Show ruler </button> </div> </ng-template> ``` Now the component's `showRuler` signal is the single source of truth: the ruler reads it, and each new menu item is initialised from it. ## What the item gives you, and what it does not | Provided by the directive | Still yours | |---|---| | `role="menuitemcheckbox"` and `aria-checked` from `checked` | the checkbox state itself | | the `cdk-menu-item` and `cdk-menu-item-checkbox` classes | a visible tick, drawn from `aria-checked` | | keyboard handling inside the menu | the action the toggle performs | | closing the menu after a pointer trigger | any styles at all | A `cdkMenuItemCheckbox` does **not** also need `cdkMenuItem`; it provides itself as a menu item. ## Radio items and groups A `cdkMenu` groups its `cdkMenuItemRadio` children as one set, so only one can be checked. For two unrelated sets in one menu, such as zoom level and page width, wrap each in a `cdkMenuGroup`. Radio items forget their state exactly like checkboxes, so bind `cdkMenuItemChecked` on each. ## Related details worth knowing - **`cdkMenuTriggerData`** passes a context object into the menu template, useful when one template serves several triggers. - **`cdkMenuOpened` and `cdkMenuClosed`** on the trigger report the menu's lifecycle; they are not a place to rescue lost item state. - **v22 rename.** The service that tracks the last open menu trigger across the app, `ContextMenuTracker`, was renamed `MenuTracker` in 22.0.0; code that injected the old name must update. - **Inline menus keep state** between interactions because their view is never destroyed, which is why the bug appears only in pop-up menus. ## Context menus behave the same way A container marked `cdkContextMenuTriggerFor` opens its menu template on the browser's native `contextmenu` event, next to the cursor, and when context-menu containers are nested the closest one wins. Under the hood it is the same kind of trigger: the template becomes a fresh view on every right-click, so a checkbox in a right-click menu on the file explorer, such as **Pin to sidebar**, needs the same `cdkMenuItemChecked` binding. ## How Angular Aria compares Angular Aria's `ngMenuItem` takes a `role` input (`'menuitem'`, `'menuitemcheckbox'` or `'menuitemradio'`). Its menus also render content lazily through `ng-template ngMenuContent`, so the same discipline applies: keep checked state in your component and bind it into the item rather than trusting the rendered element to remember.
- How do you keep two separate radio sets, zoom level and page width, inside one CDK View menu?By default a `cdkMenu` treats all its `cdkMenuItemRadio` children as one group, so checking a page width would uncheck the zoom level. Wrap each set in its own `cdkMenuGroup` element. Bind `cdkMenuItemChecked` on every radio item from component state, because radio items forget their state on reopen exactly like checkbox items.
- Why does the same checkbox keep its state in an inline `cdkMenu` placed in a sidebar?An inline menu is ordinary page content: its view is created once and lives as long as the page, so the item's locally flipped `checked` survives between interactions. Only pop-up menus, attached from an `ng-template` on each open, re-create their items. Binding `cdkMenuItemChecked` is still the right design, because other code may change the setting too.
A pop-up menu is a whiteboard in a meeting room that is wiped after every meeting. Ticking a box on it works until the room is cleared; to see the tick next time, keep it in your own notebook, the component, and copy it back onto the board when the meeting starts.
- A cdkMenuItemCheckbox stores its checked state across menu openings
- The CDK menu caches a pop-up menu's view between openings
- A cdkMenuItemCheckbox also needs cdkMenuItem to work
- cdkMenuItemCheckbox supports two-way binding through [(checked)]
- The CDK menu draws a check mark for checked items