{"record":{"id":"c2c9fb2832e81025","repo":"angular/components","slug":"attempting-to-attach-dialog-content-after-content","errorCode":null,"errorMessage":"Attempting to attach dialog content after content is already attached","messagePattern":"Attempting to attach dialog content after content is already attached","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/cdk/dialog/dialog-container.ts","lineNumber":46,"sourceCode":"  Component,\n  ComponentRef,\n  ElementRef,\n  EmbeddedViewRef,\n  Injector,\n  NgZone,\n  OnDestroy,\n  Renderer2,\n  ViewChild,\n  ViewEncapsulation,\n  afterNextRender,\n  inject,\n  DOCUMENT,\n} from '@angular/core';\nimport {DialogConfig, DialogContainer} from './dialog-config';\nimport {Observable, Subject} from 'rxjs';\n\nexport function throwDialogContentAlreadyAttachedError() {\n  throw Error('Attempting to attach dialog content after content is already attached');\n}\n\n/**\n * Internal component that wraps user-provided dialog content.\n * @docs-private\n */\n@Component({\n  selector: 'cdk-dialog-container',\n  templateUrl: './dialog-container.html',\n  styleUrl: 'dialog-container.css',\n  encapsulation: ViewEncapsulation.None,\n  // Using OnPush for dialogs caused some G3 sync issues. Disabled until we can track them down.\n  // tslint:disable-next-line:validate-decorators\n  changeDetection: ChangeDetectionStrategy.Eager,\n  imports: [CdkPortalOutlet],\n  host: {\n    'class': 'cdk-dialog-container',\n    'tabindex': '-1',","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/dialog/dialog-container.ts#L28-L64","documentation":"The CDK Dialog's DialogContainer allows user content (a component portal or template portal) to be attached exactly once. throwDialogContentAlreadyAttachedError fires when attachComponentPortal, attachTemplatePortal, or attachDomPortal is called after content is already attached to the container. This guards against rendering two content nodes into one dialog overlay.","triggerScenarios":"Calling dialogRef/DialogContainer attach methods a second time on the same dialog, e.g. calling dialogRef.componentInstance re-attach logic, programmatically calling container.attachComponentPortal twice, or re-opening logic that attaches content to an already-populated DialogRef.","commonSituations":"Custom dialog wrappers that retry attach on error without checking if content is attached; ngDoCheck-based logic that attaches content repeatedly; reusing a single DialogRef for multiple opens.","solutions":["Attach dialog content only once per DialogRef; check container state before attaching (e.g. track whether you already attached).","Reuse the already-attached content instead of attaching again: read from dialogRef.componentInstance or the container's attached content.","If you need new content, close the dialog and call Dialog.open again to get a fresh DialogRef/DialogContainer.","Guard multiple attach calls with a flag or an `if (!container.hasAttached())`-style check."],"exampleFix":"// before\nthis.dialog.open(MyDialog).componentInstance;\ncontainer.attachComponentPortal(new ComponentPortal(OtherComp)); // throws if content attached\n// after\nif (!this.attached) {\n  container.attachComponentPortal(new ComponentPortal(OtherComp));\n  this.attached = true;\n}","handlingStrategy":"validation","validationCode":"if (container.hasAttached()) {\n  throw new Error('Dialog content already attached; skipping attach.');\n}\ncontainer.attachComponentPortal(portal);","typeGuard":"function canAttach(container: DialogContainer): boolean {\n  return !container.hasAttached();\n}","tryCatchPattern":"try {\n  container.attachComponentPortal(portal);\n} catch (e) {\n  if ((e as Error).message.includes('already attached')) {\n    // content already present; reuse existing dialog\n  } else { throw e; }\n}","preventionTips":["Track attach state per DialogRef in a flag or Set<DialogRef>.","Only attach content in one place in your wrapper (e.g. the constructor of the dialog component).","Reuse dialogRef.componentInstance instead of attaching new content.","Close and reopen a fresh dialog when content must change."],"tags":["angular","cdk","dialog","lifecycle"],"backgroundTag":"content-already-attached","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}