{"record":{"id":"6853c65b4a5eb8bd","repo":"angular/components","slug":"this-position-strategy-is-already-attached-to-an-o","errorCode":null,"errorMessage":"This position strategy is already attached to an overlay","messagePattern":"This position strategy is already attached to an overlay","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/cdk/overlay/position/flexible-connected-position-strategy.ts","lineNumber":195,"sourceCode":"\n  constructor(\n    connectedTo: FlexibleConnectedPositionStrategyOrigin,\n    private _viewportRuler: ViewportRuler,\n    private _document: Document,\n    private _platform: Platform,\n    private _overlayContainer: OverlayContainer,\n  ) {\n    this.setOrigin(connectedTo);\n  }\n\n  /** Attaches this position strategy to an overlay. */\n  attach(overlayRef: OverlayRef): void {\n    if (\n      this._overlayRef &&\n      overlayRef !== this._overlayRef &&\n      (typeof ngDevMode === 'undefined' || ngDevMode)\n    ) {\n      throw Error('This position strategy is already attached to an overlay');\n    }\n\n    this._validatePositions();\n\n    overlayRef.hostElement.classList.add(boundingBoxClass);\n\n    this._overlayRef = overlayRef;\n    this._boundingBox = overlayRef.hostElement;\n    this._pane = overlayRef.overlayElement;\n    this._isDisposed = false;\n    this._isInitialRender = true;\n    this._lastPosition = null;\n    this._resizeSubscription.unsubscribe();\n    this._resizeSubscription = this._viewportRuler.change().subscribe(() => {\n      // When the window is resized, we want to trigger the next reposition as if it\n      // was an initial render, in order for the strategy to pick a new optimal position,\n      // otherwise position locking will cause it to stay at the old one.\n      this._isInitialRender = true;","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/overlay/position/flexible-connected-position-strategy.ts#L177-L213","documentation":"A FlexibleConnectedPositionStrategy can only be attached to a single OverlayRef. attach() throws if the strategy already has an _overlayRef and a different overlay is being attached. The strategy stores its overlay ref to compute positions; sharing it across overlays would corrupt that state.","triggerScenarios":"Reusing one position strategy object (created via overlay.position().flexibleConnectedTo(origin)) for a second overlay.create() call; caching a strategy in a service and attaching it to a new overlay after the first.","commonSituations":"Opening multiple tooltips/popovers that share a prebuilt strategy; recreating an overlay without rebuilding the strategy; retry logic that re-calls overlay.create with the same strategy instance.","solutions":["Create a fresh strategy per overlay: call overlay.position().flexibleConnectedTo(origin) each time you create an overlay.","If the same overlay should be reused, keep the original OverlayRef and call updatePosition() instead of attaching again.","Dispose the old overlay (overlayRef.dispose()) before attaching the strategy to a new one — but prefer building a new strategy.","Move strategy construction inside your open() method so it is never shared across overlay instances."],"exampleFix":"// before\nthis.strategy = this.overlay.position().flexibleConnectedTo(origin);\nthis.overlay.create({ positionStrategy: this.strategy }); // first\nthis.overlay.create({ positionStrategy: this.strategy }); // throws\n// after\nconst strategy = this.overlay.position().flexibleConnectedTo(origin);\nthis.overlayRef = this.overlay.create({ positionStrategy: strategy });","handlingStrategy":"fallback","validationCode":"if (!strategyRef || strategyRef.hasAttached()) {\n  strategyRef = overlay.position().flexibleConnectedTo(origin);\n}\nconst ref = overlay.create({ positionStrategy: strategyRef });","typeGuard":null,"tryCatchPattern":"try {\n  overlayRef = overlay.create({ positionStrategy: strategy });\n} catch (e) {\n  if ((e as Error).message.includes('already attached to an overlay')) {\n    overlayRef = overlay.create({ positionStrategy: overlay.position().flexibleConnectedTo(origin) });\n  } else { throw e; }\n}","preventionTips":["Create a new position strategy for every overlay.create call.","Never cache FlexibleConnectedPositionStrategy instances in services for reuse.","Call updatePosition() on the existing overlay instead of re-attaching.","Dispose overlays promptly so strategies are not reused after detach."],"tags":["angular","cdk","overlay","position-strategy"],"backgroundTag":"strategy-already-attached","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}