我正在尝试为表格单元格制作自定义弹出窗口,以便在单击单元格时显示单元格详细信息,其方式类似于 mdBoostrap popovers .
目前,我有以下应用程序:https://stackblitz.com/edit/angular-m5rx6j
弹出组件显示在主组件下方,但我想将其显示在表格上方,就在我单击的元素下方。
我想我需要执行以下操作:
- 获取我点击的“td”的 ElementRef -> 我不知道如何
- 将覆盖层附加到此元素 -> 已经这样做了,但使用的是根元素
最佳答案
Netanet Basal 的博客中有两篇关于使用 CDK 的 OverLay 的精彩文章
我尝试简化为 this stackblitz
基本上你有一个注入(inject) Overlay 的服务
constructor(private overlay: Overlay) { }
要打开模板,您需要传递原点(我称他为“原点”)、模板(我称其为菜单)和组件的 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
}));
getOverLayConfig 返回类似的配置
private getOverlayConfig({ origin}): OverlayConfig {
return new OverlayConfig({
hasBackdrop: false,
backdropClass: "popover-backdrop",
positionStrategy: this.getOverlayPosition(origin),
scrollStrategy: this.overlay.scrollStrategies.close()
});
}
位置策略是您想要附加模板的位置 - 带有您首选位置的数组,例如
[
{
originX: "center",
originY: "bottom",
overlayX: "center",
overlayY: "top"
},
]
嗯,代码的另一部分是关于关闭模板元素的。我选择在服务中创建一个打开该功能的函数
1.-附加元素
2.-创建订阅
this.sub = fromEvent<MouseEvent>(document, "click")
3.-返回一个可观察量,该可观察量返回 null 或您在函数“close”中传递的参数“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>
更新如果我们想首先附加一个组件,我们需要记住它必须位于模块的entryComponents中
@NgModule({
imports: [ BrowserModule, FormsModule,OverlayModule ],
declarations: [ AppComponent,HelloComponent], //<--HERE
bootstrap: [ AppComponent ],
entryComponents:[HelloComponent] //<--and HERE
})
嗯,附加组件很简单,更改附加并使用 ComponentPortal,例如
const ref=this.overlayRef.attach(new ComponentPortal(HelloComponent,viewContainerRef))
然后,如果我们的组件有一些输入,例如
@Input() name="Angular";
@Input() obj={count:0};
我们可以使用 ref.instance 来访问组件,例如
ref.instance.name="New Name"
但是由于我们想要维持服务最通用的用途,我想使用参数“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...
}
一如既往,如果我们传递一个对象,组件中的所有更改都会更改该对象的属性,因此在我们的主组件中可以进行类似的操作
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)
})
}
看到,当我作为 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};
}
您可以在 new stackblitz 中看到
Update2 要从 HelloComponent 关闭面板,我们需要将服务注入(inject)为公共(public)服务并使用 close。或多或少,一个按钮
<button (click)="popupService.close(4)">close</button>
在哪里注入(inject)服务
constructor(public popupService: MenuContextualService){}
关于 Angular CDK : attach overlay to a clicked element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59199540/