Search code examples
angularangular-materialtypescript2.0angular-cdk

Angular CDK:Getting error for FlexibleConnectedPositionStrategy in Overlay config


I want to configure an overlay for with overlay.position().flexibleConnectedTo() because connectedTo() is deprecated as per the official docs. Otherwise there is a quesstion having a good answer for connectedTo() enter image description here

Here is my Code

    const origin:FlexibleConnectedPositionStrategyOrigin=this.RefElem;
    const overlayConfig = new OverlayConfig();
    overlayConfig.positionStrategy = this.overlay.position().flexibleConnectedTo(origin);
    const overlayRef = this.overlay.create(overlayConfig);
    const userProfilePortal = new ComponentPortal(
      GraphMetaSignalSelectorComponent
    );
    overlayRef.attach(userProfilePortal);

but getting this error: "ConnectedToFlexibleConnectedPositionStrategy: At least one position is required. at FlexibleConnectedPositionStrategy.push"


Solution

  • For those curious who stuck with the accepted answer because of the lack of the implementation of this.getPositions(), here's a quick sample for copy-pasting:

    const positionStrategy = this.overlay.position()
          .flexibleConnectedTo(origin)
          .withPositions([{
            // here, top-left of the overlay is connected to bottom-left of the origin; 
            // of course, you can change this object or generate it dynamically;
            // moreover, you can specify multiple objects in this array for CDK to find the most suitable option
            originX: 'start',
            originY: 'bottom',
            overlayX: 'start',
            overlayY: 'top'
          } as ConnectedPosition])
          .withPush(false); // or true, if you want to push the overlay into the screen when it doesn't fit