Angular CDK:将叠加层附加到单击的元素

Dre*_*mor 3 overlay angular angular-cdk

我正在尝试为表格单元格制作自定义弹出框,以便在单击时显示单元格详细信息,其方式类似于mdBoostrap popovers

现在,我有以下应用程序:https : //stackblitz.com/edit/angular-m5rx6j

Popup 组件显示在主组件下,但我想将它显示在表格上方,就在我单击的元素下方。

我想我需要执行以下操作:

  • 获取我单击的“td”的 ElementRef -> 我不知道如何
  • 将覆盖层附加到此元素 -> 已经这样做了,但是使用根元素

Eli*_*seo 9

Netanet Basal 的博客中有两篇关于使用来自 CDK 的 OverLay 的精彩文章

  1. 使用 Angular CDK 创建强大的组件
  2. 使用 Angular CDK 轻松创建上下文菜单

我试图在这个堆栈闪电战中简化

基本上你有一个注入覆盖的服务

constructor(private overlay: Overlay) { }
Run Code Online (Sandbox Code Playgroud)

要打开模板,您需要传递原点(我称他为“原点”)、模板(我称其为菜单)和组件的 viewContainerRef

    this.overlayRef = this.overlay.create(
        this.getOverlayConfig({ origin: origin})
    );
    //I can pass "data" as implicit and "close" to close the menu
    this.overlayRef.attach(new TemplatePortal(menu, viewContainerRef, {
        $implicit: data, close:this.close
    }));
Run Code Online (Sandbox Code Playgroud)

getOverLayConfig 返回一个类似的配置

private getOverlayConfig({ origin}): OverlayConfig {
    return new OverlayConfig({
        hasBackdrop: false,
        backdropClass: "popover-backdrop",
        positionStrategy: this.getOverlayPosition(origin),
        scrollStrategy: this.overlay.scrollStrategies.close()
    });
}
Run Code Online (Sandbox Code Playgroud)

位置策略是您要附加模板的位置 - 具有您首选位置的数组,例如

      [
        {
            originX: "center",
            originY: "bottom",
            overlayX: "center",
            overlayY: "top"
        },
      ]
Run Code Online (Sandbox Code Playgroud)

那么,代码的另一部分是关于关闭模板元素。我选择在服务中创建一个打开的函数

1.-附加元素

2.-创建订阅

this.sub = fromEvent<MouseEvent>(document, "click")
Run Code Online (Sandbox Code Playgroud)

3.-返回一个返回 null 的 observable 或您在函数“close”(*) 中传递的参数

注意:不要忘记包含在你的 css 中 ~@angular/cdk/overlay-prebuilt.css

(*) 这让我的模板像

<ng-template #tpl let-close="close" let-data>
  <div class="popover" >
    <h5>{{name}} {{data.data}}</h5> //<--name is a variable of component
                                    //data.data a variable you can pass
  And here's some amazing content. It's very engaging. Right?
  <div>
   <a (click)="close('uno')">Close</a> //<--this close and return 'uno'
  </div>
  </div>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

更新如果我们要先附加一个组件,我们需要记住它必须在模块的 entryComponents 中

@NgModule({
  imports:      [ BrowserModule, FormsModule,OverlayModule ],
  declarations: [ AppComponent,HelloComponent], //<--HERE
  bootstrap:    [ AppComponent ],
  entryComponents:[HelloComponent]  //<--and HERE

})
Run Code Online (Sandbox Code Playgroud)

好吧,附加组件很简单,更改附加并使用 ComponentPortal,例如

const ref=this.overlayRef.attach(new ComponentPortal(HelloComponent,viewContainerRef))
Run Code Online (Sandbox Code Playgroud)

然后,如果我们的组件有一些输入,例如

  @Input() name="Angular";
  @Input() obj={count:0};
Run Code Online (Sandbox Code Playgroud)

我们可以使用 ref.instance 来访问组件,例如

  ref.instance.name="New Name"
Run Code Online (Sandbox Code Playgroud)

但是由于我们想要维护服务的最普遍用途,我想使用参数“data”来为变量赋值,所以我们的函数“open”变成了

open(origin: any, component: any, viewContainerRef: ViewContainerRef, data: any) {
        this.close(null);
        this.overlayRef = this.overlay.create(
            this.getOverlayConfig({ origin: origin})
        );
        const ref=this.overlayRef.attach(new ComponentPortal(component, viewContainerRef));
    for (let key in data) //here pass all the data to our component
    {
       ref.instance[key]=data[key]
    } 
    ...rest of code...
}
Run Code Online (Sandbox Code Playgroud)

和往常一样,如果我们传递一个对象,组件中的所有变化都会改变对象的属性,所以在我们的主组件中可以做一些像

obj={count:2}

open(origin:any,menu:any,index:number)
  {
    this.popupService.open(origin,HelloComponent,this.viewContainerRef,
        {name:'new Name'+index,obj:this.obj})
    .subscribe(res=>{
      console.log(res)
    })
  }
Run Code Online (Sandbox Code Playgroud)

看到,当我将对象作为 obj 传递时,组件中的任何更改都会更改对象的属性,在我的情况下,组件非常简单

@Component({
  selector: 'component',
  template:`Hello {{name}}
    <button (click)="obj.count=obj.count+1">click</button>
  `
})
export class HelloComponent  {
  @Input() name="Angular";
  @Input() obj={count:0};
}
Run Code Online (Sandbox Code Playgroud)

您可以在新的堆栈闪电战中看到

Update2要从 HelloComponent 关闭面板,我们需要将服务注入为 public 并使用 close。或多或少,一个按钮

<button (click)="popupService.close(4)">close</button>
Run Code Online (Sandbox Code Playgroud)

注入服务的地方

constructor(public popupService: MenuContextualService){}
Run Code Online (Sandbox Code Playgroud)