components icon indicating copy to clipboard operation
components copied to clipboard

No provider for MatDialogRef!

Open ghost opened this issue 9 years ago • 34 comments

constructor(public dialog: DialogRef, public dialogRef: MatDialogRef )

Gives error 'No provider for MatDialogRef!'

ghost avatar Oct 11 '17 17:10 ghost

Have you added it to your module as a provider? Next to that: Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are stack overflow, gitter, mailing list, etc.

donroyco avatar Oct 11 '17 17:10 donroyco

I tried to add MatDialogRef in providers array of module, but it gives an error 'can't resolve all parameters for MatdialogRef'

ghost avatar Oct 11 '17 18:10 ghost

Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are stack overflow, gitter, mailing list, etc.

jelbourn avatar Oct 11 '17 18:10 jelbourn

@darshu514 are you find any solution of this issue No provider for MatDialogRef

praveeno avatar Oct 22 '17 10:10 praveeno

Do you have a solution for this problem?

dennisvanbets avatar Oct 22 '17 18:10 dennisvanbets

No, I think it’s an open issue. I found a work around by using https://github.com/shlomiassaf/ngx-modialog

The implementation is pretty simple

ghost avatar Oct 23 '17 01:10 ghost

got it, in my case there is two component one is DialogComponent and another is AddComponent from we open the dialog e.g , and i accidentally init DialogComponent without any provider like AddComponent, if i init DialogComponent from AddComponent like, this.dialog.open(DialogComponent',{}) , then it not give any error.

praveeno avatar Oct 23 '17 09:10 praveeno

Same here, still no solution?

mkamrani avatar Oct 27 '17 01:10 mkamrani

@mkamrani create a question on stackoverflow, we all try to help

praveeno avatar Oct 27 '17 02:10 praveeno

Hey, Please try import MatDialog from @angular/material and provide it into providers array inside appModule. Its worked for me.

vasylsemiliak avatar Oct 28 '17 18:10 vasylsemiliak

@unknown4unnamed +1

praveeno avatar Nov 07 '17 11:11 praveeno

You will get that exact "No provider for MatDialogRef" error in case you include your dialog's component html selector in any html file.

<dialog-html-selector></dialog-html-selector>

There is no need for that, do not include the html selector of the dialog component anywhere. You will get the error otherwise

javieraviles avatar Dec 13 '17 13:12 javieraviles

Is it possible to have a component that opens in a dialog on some occasions and also renders using the html selector on other occasions? I am trying to make a feature of my application responsive and when the display is small I want a dialog otherwise I want to render it with the html selector.

ajspeller avatar Dec 13 '17 18:12 ajspeller

The thing is that your component is made for being loaded in a MatDialog because in your *.ts file you are surely getting in your constructor a MatDialogRef:

A dialog is opened by calling the open method with a component to be loaded and an optional config object. The open method will return an instance of MatDialogRef:

let dialogRef = dialog.open(UserProfileComponent, {
  height: '400px',
  width: '600px',
});
The MatDialogRef provides a handle on the opened dialog. It can be used to close the dialog and to receive notification when the dialog has been closed.

dialogRef.afterClosed().subscribe(result => {
  console.log(`Dialog result: ${result}`); // Pizza!
});

dialogRef.close('Pizza!');

Maybe you can have a more general component specific for loading dialogs with the structure of a dialog in the dom with "mat-dialog-title", "mat-dialog-content", "mat-dialog-actions"... Receiving in the data (MAT_DIALOG_DATA) the parameters of title and actions and then within the content section of the dialog you can load the component you want to use also in the DOM with the selector??

I am not an angular expert but it is the best idea I can think of now.

javieraviles avatar Dec 13 '17 20:12 javieraviles

I removed the dialog component selector and it worked fine! :) It was like this:

@Component({ selector: 'app-users-dialog', templateUrl: './users-dialog.component.html', styleUrls: ['./users-dialog.component.css'] })

And I changed to: @Component({ templateUrl: './users-dialog.component.html', styleUrls: ['./users-dialog.component.css'] })

marylly avatar Jan 05 '18 12:01 marylly

@marylly Thank you, same here. It's kind of logic if you think about there is no selector for the modal content, but I haven't found any documentation about it. Thanks again!

Ghost990 avatar Jan 05 '18 21:01 Ghost990

Can anyone please Help me with this?

compiler.js?dd9e:485 Uncaught Error: Unexpected value 'MatDialogRef' imported by the module 'AppModule'. Please add a @NgModule annotation.

Ukmandal avatar Jan 24 '18 07:01 Ukmandal

@Ukmandal could you please provide your app.module.ts? Or the correspondant module of the view your are trying to load the modal in? I think you are declaring MatDialogRef on it or something like that instead of the MatDialogModule

javieraviles avatar Jan 24 '18 07:01 javieraviles

I'm having the same issue. I have a form component which sometimes I need to display in a modal dialog and sometimes as an independent page (by navigation). It works fine in the modal but when I try to navigate to that page, I get this error "NullInjectorError: No provider for MatDialogRef!"

sainture avatar Feb 01 '18 04:02 sainture

For me, none of these solutions worked. However, what did work for me was stubbing out the MatDialogRef. You solve you problem like this: Below your imports: const matDialogRefStub = {};

Add this to the providers section of your testbed config: {provider: MatDialogRef, useValue: matDialogRefStub}

Also, don't forget to have MatDialog as a provider as well.

That should be it, goodluck!

cole21771 avatar Feb 09 '18 22:02 cole21771