I had to pass the overlayRef manually, is there a nicer way to include it in the components constructor with DI?
service code:
display() {
const overlayRef = this.overlay.create({
positionStrategy: this.overlay.position().global().top(),
});
const portal = new ComponentPortal(ErrorsListBottomSheetComponent);
this.ref = overlayRef.attach<ErrorsListBottomSheetComponent>(portal);
this.ref.instance.overlayRef = overlayRef;
overlayRef.backdropClick().subscribe(() => {
overlayRef.detach();
});
}
component code:
export class ErrorsListBottomSheetComponent implements OnInit {
overlayRef: OverlayRef; -- REMOVE THIS--
constructor(
-- ADD SOMETHING HERE --
) {}
close() {
if (this.overlayRef) {
this.overlayRef.detach();
}
}
better yet, is there something for overlay similar to
<button mat-dialog-close>close</button>
instead of
<button (click)="close()">close</button>
You can use @Ouput event emitter to emit event from component and then subscribing it in service.
Component Code:
export class ErrorsListBottomSheetComponent implements OnInit {
@Output() closePanel = new EventEmitter<void>();
...
close() {
this.closePanel.emit();
}
}
Service code:
display() {
...
this.ref = overlayRef.attach<ErrorsListBottomSheetComponent>(portal);
this.ref.instance.closePanel.subscribe(() => overlayRef.detach());
}
Html Code:
<button (click)="close()">Close</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With