3

I reoponed this issue since it didn't solve my problem in a similar discusion.

I still have the error 'No provider for MdDialogRef' even after i followed the official tutorial step by step.

I have two components. First component :

import { MdDialog} from "@angular/material";

import { DocumentDialogComponent } from './document-dialog.component';    

@Component({
  selector: 'documents-list',
  template
})
export class DocumentsListComponent {

  constructor(
     public dialog: MdDialog) {
  }

  openFormDialog() {

   let dialogRef  = this.dialog.open(DocumentDialogComponent,
   {
   }
   );
   dialogRef.afterClosed().subscribe(result => {
      this.selectedOption = result;
   });
  }

My second component (The Dialog) :

import { MdDialogRef} from "@angular/material";

@Component({
 selector: 'document-dialog',
 template
 })

export class DocumentDialogComponent {
    constructor(
        public dialogRef: MdDialogRef<DocumentDialogComponent>
    ) {}
}

And my module config :

  import { MaterialModule } from "@angular/material";
  import { DocumentsListComponent } from './documents-list.component';
  import { DocumentDialogComponent } from './document-dialog.component';

  imports : [
    MaterialModule.forRoot() 
   ],

  declarations: [
      AppComponent,
      DocumentListComponent,
      DocumentDialogComponent
    ],
    entryComponents: [
      AppComponent,
      DocumentListComponent,
      DocumentDialogComponent
    ],
    providers: [
    ],
    bootstrap: [
      AppComponent
    ]

Why i still have the error :

Error in ./DocumentsListComponent class DocumentsListComponent - inline template:0:167 caused by: No provider for MdDialogRef!
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Kivo
  • 385
  • 8
  • 24

2 Answers2

4

I removed the <DocumentsListComponent></DocumentsListComponent> tag inside my template and now it works.

Pang
  • 9,564
  • 146
  • 81
  • 122
Kivo
  • 385
  • 8
  • 24
2

We can eliminate this error by setting reference in DocumentDialogComponent using componentInstance property.

Rather than injecting the MdDialogRef in to the component we can set its property by componentInstance of returned reference from open method.

Here is the modified working code:

import { MdDialog} from "@angular/material";

import { DocumentDialogComponent } from './document-dialog.component';    

@Component({
  selector: 'documents-list',
  template
})
export class DocumentsListComponent {

  constructor(
     public dialog: MdDialog) {
  }

  openFormDialog() {

   let dialogRef  = this.dialog.open(DocumentDialogComponent);

    //set the reference here
    dialogRef.componentInstance.dRef = dialogRef;

   dialogRef.afterClosed().subscribe(result => {
      this.selectedOption = result;
   });
  }

The dialog component is here:

import { MdDialogRef} from "@angular/material";

@Component({
 selector: 'document-dialog',
 template
 })

export class DocumentDialogComponent {
    public dRef:MdDialogRef<DocumentDialogComponent>
    constructor() {}
}
rsirs
  • 107
  • 7