Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement MAT_DIALOG_DEFAULT_OPTIONS?

On a project we multiple Dialog's. Now, I want to set samen global variables to the DialogOption.

I found: https://material.angular.io/components/dialog/overview and this code:

@NgModule({
  providers: [
    {provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {hasBackdrop: false}}
  ]
})

I applied these code with some other, but it doesn't work. The settings doesn't applied at all.

Does anybody get these setting worked?

like image 382
Johan Walhout Avatar asked Sep 10 '18 10:09

Johan Walhout


People also ask

How do you use mat dialog?

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.

How do you make Mat dialog resizable?

The resizable dialog can be created by setting the enableResize property to true, which is used to change the size of a dialog dynamically and view its content with expanded mode. The resizeHandles property can also be configured for all the which directions in which the dialog should be resized.

How do I import MatDialogModule?

In order to use the dialog component, we need to import the MatDialogModule module into the app module like this. import { MatDialogModule } from '@angular/material/dialog'; After importing the dialog module, the next step is to include it inside the imports array as given below.


2 Answers

Angular Material provides practical defaults for several of its config options, such as closeOnNavigation, autoFocus, etc.

If you only want to overwrite a few of the config items, while leaving the rest as their defaults, you can provide MAT_DIALOG_DEFAULT_OPTIONS like this:


import {MAT_DIALOG_DEFAULT_OPTIONS, MatDialogConfig} from '@angular/material';

...

  providers: [
    {
      provide: MAT_DIALOG_DEFAULT_OPTIONS,
      useValue: {
        ...new MatDialogConfig(),
        hasBackdrop: false,
      } as MatDialogConfig,
    }
  ]

...

With this approach, you only need to specify the config options you want to change. The rest will take their default values.

like image 62
Alex K Avatar answered Oct 09 '22 04:10

Alex K


That works... Go here and add these to verify it.

import {MAT_DIALOG_DEFAULT_OPTIONS} from '@angular/material';

....

providers: [
  {provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {hasBackdrop: false}}
]
like image 43
Fartab Avatar answered Oct 09 '22 04:10

Fartab