{"record":{"id":"f5b631732cf5b2ba","repo":"angular/components","slug":"cdkselectall-missing-cdkselection-in-the-parent-f5b631","errorCode":null,"errorMessage":"CdkSelectAll: missing CdkSelection in the parent","messagePattern":"CdkSelectAll: missing CdkSelection in the parent","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/cdk-experimental/selection/selection-toggle.ts","lineNumber":79,"sourceCode":"    this.checked = _selection.change.pipe(\n      switchMap(() => observableOf(this._isSelected())),\n      distinctUntilChanged(),\n    );\n  }\n\n  ngOnInit() {\n    this._assertValidParentSelection();\n    this._configureControlValueAccessor();\n  }\n\n  ngOnDestroy() {\n    this._destroyed.next();\n    this._destroyed.complete();\n  }\n\n  private _assertValidParentSelection() {\n    if (!this._selection && (typeof ngDevMode === 'undefined' || ngDevMode)) {\n      throw Error('CdkSelectAll: missing CdkSelection in the parent');\n    }\n  }\n\n  private _configureControlValueAccessor() {\n    if (this._controlValueAccessors && this._controlValueAccessors.length) {\n      this._controlValueAccessors[0].registerOnChange((e: unknown) => {\n        if (typeof e === 'boolean') {\n          this.toggle();\n        }\n      });\n\n      this.checked.pipe(takeUntil(this._destroyed)).subscribe(state => {\n        this._controlValueAccessors![0].writeValue(state);\n      });\n    }\n  }\n\n  private _isSelected(): boolean {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk-experimental/selection/selection-toggle.ts#L61-L97","documentation":"CdkSelectAll/CdkSelectToggle (selection-toggle.ts) requires a parent CdkSelection whose SelectionSet it operates on. ngOnInit calls _assertValidParentSelection, which throws in dev mode when the injected parent selection (_selection) is missing — i.e., the toggle directive was used outside a CdkSelection container. Without it, the toggle has no selection model to drive.","triggerScenarios":"Placing <cdk-select-all> (or the toggle directive) in a template with no ancestor <cdk-selection> / [cdkSelection] directive; putting it inside a CDK table row whose parent selection lives in an unrelated component; typos in selector usage that skip the wrapper.","commonSituations":"Moving the select-all checkbox out of the CdkSelection template during refactor; rendering rows with a custom component that does not project the toggle under CdkSelection; forgetting that the directive resolves the parent via content/parent injection, not a service.","solutions":["Wrap the toggle in a CdkSelection: place it inside the <cdk-selection> element that has dataSource set.","If it must live elsewhere, hoist the CdkSelection to a common ancestor component template.","Verify the injected parent: ensure the directive's selector matches the CdkSelection host (parent/content based DI)."],"exampleFix":"<!-- before -->\n<my-toolbar><cdk-select-all>...</cdk-select-all></my-toolbar>\n\n<!-- after -->\n<cdk-selection [dataSource]=\"rows\">\n  <my-toolbar><cdk-select-all>...</cdk-select-all></my-toolbar>\n</cdk-selection>","handlingStrategy":"type-guard","validationCode":"const parentSelection: CdkSelection<unknown> | null = inject(CdkSelection, { optional: true });\nif (!parentSelection) throw new Error('<cdk-select-all> must be used inside <cdk-selection>');","typeGuard":"function hasParentSelection(dir: { _selection: CdkSelection<unknown> | null | undefined }): boolean {\n  return dir._selection != null;\n}","tryCatchPattern":"ngOnInit() {\n  try {\n    this._assertValidParentSelection();\n  } catch (e) {\n    console.error('Place this directive inside a CdkSelection element.', e);\n  }\n}","preventionTips":["Keep <cdk-select-all> and toggle directives inside the <cdk-selection> element.","If the injector cannot see the parent (component boundary), hoist CdkSelection or pass the selection explicitly.","Check rendering in dev mode — the assertion fires on init."],"tags":["angular","cdk","selection","missing-parent","dev-mode-assertion"],"backgroundTag":"directive-used-outside-parent","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}