useDialog
Generic types: | T |
Assembles a facade object for a specific dialog component type with optional default configurations, enabling a satisfying development experience working with Angular CDK dialogs.
Presentation
function useDialog (
component: Type <T>,
configDefaults: Partial<DialogConfigOf <T>> = {},
[service, injector, destroyRef, componentFactoryResolver, viewContainerRef]: [
Dialog,
Injector ,
DestroyRef ,
ComponentFactoryResolver ,
ViewContainerRef | undefined,
] = [
inject (Dialog),
inject (Injector ),
inject (DestroyRef ),
inject (ComponentFactoryResolver ),
inject (ViewContainerRef , { optional: true }) ?? undefined, // available only in components
],
): DialogFacade <T>;
Returns
Parameters
Name | Type | Description |
---|---|---|
component |
| dialog component class, extending |
configDefaults | Partial< | default configuration for the dialog |
[ service, injector, destroyRef, componentFactoryResolver, viewContainerRef, ] | [Dialog, |
Example usage
@Component ({ ... })
class HostComponent {
private myDialog = useDialog (MyDialog, { disableClose: true });
onFoo(): void {
// Typed dialog launching experience.
const ref = this.myDialog.launch({ data: { inputs: "values" } });
// Typed dialog output
ref.closed.subscribe((output ) => { ... });
}
onBar(): void {
// Access the current DialogRef anytime.
this.myDialog.ref()?.close();
}
constructor() {
effect (() => {
const ref = this.myDialog.ref();
// Listen to the dialog launching and closing
if (ref) console.log("Dialog launched");
else console.log("Dialog closed or never launched");
});
}
}