最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular CDK Overlay, change default overlay container - Stack Overflow

programmeradmin6浏览0评论

Is there a way to change the OverlayContainer?

I have created a tooltip ponent, but sometimes I want to attach the overlay to a specific element (by default the overlay is attached to the document body).

Here is how I am creating the overlay:

  private initOverlay(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions([this.resolvedConfig]);

    this.overlayRef = this.overlay.create({positionStrategy});
  }

And this is how I am attaching a template to it:

  show(): void {
    this.overlayRef.attach(new TemplatePortal(this.tpl, this.viewContainerRef));
  }

Is there a way to change the OverlayContainer?

I have created a tooltip ponent, but sometimes I want to attach the overlay to a specific element (by default the overlay is attached to the document body).

Here is how I am creating the overlay:

  private initOverlay(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions([this.resolvedConfig]);

    this.overlayRef = this.overlay.create({positionStrategy});
  }

And this is how I am attaching a template to it:

  show(): void {
    this.overlayRef.attach(new TemplatePortal(this.tpl, this.viewContainerRef));
  }
Share Improve this question edited Dec 28, 2020 at 10:13 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Mar 21, 2019 at 20:10 vlio20vlio20 9,29518 gold badges101 silver badges186 bronze badges 2
  • What's your use case for attaching it to a specific element? – adamdport Commented Mar 21, 2019 at 20:38
  • 1 I have a dialog, the tooltip appears behind it (the tooltip trigger is placed on the dialog). – vlio20 Commented Mar 21, 2019 at 20:40
Add a ment  | 

1 Answer 1

Reset to default 16

Please reference this stackblitz example.

looks like you can acplish this by extending the OverlayContainer class via the following in app.module.ts

{ provide: OverlayContainer, useFactory: () => new AppOverlayContainer() }

Stackblitz

https://stackblitz./edit/angular-material2-issue-ansnt5?file=app%2Fapp.module.ts


This GitHub ment also provides an example of how to package this in a directive

GitHub ment

https://github./angular/material2/issues/7349#issuement-337513040


Revision 3/22/19 working directive example

Extend the OverlayContainer class via cdk-overlay-container.ts

Stub the class in app.module.ts

  providers: [
    { provide: OverlayContainer, useClass: CdkOverlayContainer },
  ]

In your cdk-overlay-container.ts you are preventing the default _createContainer() from working, and providing your own custom public method myCreateContainer to replace it.

You are essentially creating an empty div here, adding a custom class to it my-custom-overlay-container-class and appending it to the div the directive is attached to, then passing that container to the private variable _containerElement in the true OverlayContainer class.

/**
* Create overlay container and append to ElementRef from directive
*/ 
public myCreateContainer(element: HTMLElement): void {
   let container = document.createElement('div');
   container.classList.add('my-custom-overlay-container-class');

   element.appendChild(container);
   this._containerElement = container;
 }
 /**
  * Prevent creation of the HTML element, use custom method above
  */
 protected _createContainer(): void {
     return;
 }

Then in your cdk-overlay-container.directive.ts your are calling myCreateContainer() and passing the ElementRef as an argument.

 this.cdkOverlayContainer['myCreateContainer'](this.elementReference.nativeElement);

Then in your HTML assign the directive where you want it to show up.

<div myCdkOverlayContainer 

Stackblitz

https://stackblitz./edit/angular-material2-issue-6nzwws?embed=1&file=app/app.ponent.html

发布评论

评论列表(0)

  1. 暂无评论