Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular material dialog - pass types into dialog component

I'm working on data driven app and currently I'm facing a problem of passing types into material angular dialog. I want to create reusable dialog form and need to change types for new dialog instance.

Is there a way to pass type into material dialog or into component? Or maybe there is possibility to create types in dialog itself from string passed as data?

I want to use types in dialog component like this (or similar):

export class DialogDynamicItemManagerDialog<T> {

    public dialogName: string;
    public items: Array<T>;
    public selectedItem: T;
    ...
}

I've tried to pass the type like this:

 OpenDynamicDialog(): void {
    this.dialog.open(DialogDynamicItemManagerDialog<MyType>, {
      data: {
        title: 'Manage items',
        items: this.items
      },
    });
}

but obviously it doesn't work.

I've also tried this:

 OpenDynamicDialog(): void {
    const dialogRef = this.dialog.open(DialogDynamicItemManagerDialog, {
      data: {
        title: 'Manage items',
        items: this.items,
        itemType: itemType
      },
    });
}

but I haven't find a way to change string into type in dialog afterwards.

like image 237
Paula Avatar asked Apr 12 '19 11:04

Paula


People also ask

How pass data from dialog to component?

You can pass any kind of data to the component by adding data to the MatDialog's open() options. }, }); You can retrieve the data by injecting MAT_DIALOG_DATA in the opened component.

How do you pass data in angular material dialog?

We can pass data to the dialog component by using the data property of the dialog configuration object. As we can see, the whole data object initially passed as part of the dialog configuration object can now be directly injected into the constructor.

What is MatDialogRef?

The MatDialogRef provides a handle on the opened dialog. It can be used to close the dialog and to receive notifications when the dialog has been closed. Any notification Observables will complete when the dialog closes. dialogRef. afterClosed().

How do I use MatDialog?

Approach: First we need to import 'MatDialog' from '@angular/material/dialog' and we need to create an instance for it in the constructor. Using this instance we can open the dialog box component. Now create a separate component for the dialog and write code as per the requirements.


1 Answers

TL;DR:

You can use the following code to specify the type that your dialog's data will use:

import { MatDialog } from '@angular/material/dialog';

export class MyComponent {
  constructor(private dialog: MatDialog) { }

  openDialog() {
    this.dialog.open<MyDialogComponent, MyType>(MyDialogComponent, {
      data: {
        // Your data goes here
      }
    };
  }
}

In your dialog component:

import { MAT_DIALOG_DATA } from '@angular/material/dialog';

export class MyDialogComponent {
  constructor(@Inject(MAT_DIALOG_DATA) public data: MyType) { }
}

The open method of the MatDialog class actually allows three types to be specified (in order of sequence):

  • T: The component class (optional - although there's no default value, you don't have to specify it if you're just calling the method without any other types)
  • D: The type to be used for the data to be added to the dialog (optional - defaults to any)
  • R: The type to be used for the result of the dialog (optional - defaults to any)

The method is defined as below:

  /**
   * Opens a modal dialog containing the given component.
   * @param componentOrTemplateRef Type of the component to load into the dialog,
   *     or a TemplateRef to instantiate as the dialog content.
   * @param config Extra configuration options.
   * @returns Reference to the newly-opened dialog.
   */
  open<T, D = any, R = any>(componentOrTemplateRef: ComponentType<T> | TemplateRef<T>,
          config?: MatDialogConfig<D>): MatDialogRef<T, R> {
    // ...
  }

The D type is then passed to MatDialogConfig, which accepts the same type to be used for the data to be passed in to the dialog.


References:

  • MatDialog#open source code - notice how the types are passed around in the code!
  • MatDialogConfig source code - notice how the class is defined!
like image 132
Edric Avatar answered Nov 02 '22 22:11

Edric