12

Angular 7.1 , AngularMaterial 7.3

I am trying to call function and pass some value, It prompt following error

No component factory found for t1. Did you add it to @NgModule.entryComponents?

Although t1 is included in entryComponent. but once remove passing value to fix value it will work.

  <button mat-button (click)="openDialog('t1')">T1</button>
  <button mat-button (click)="openDialog('t2')">T2</button>

Once I pass value its show the above code.

  openDialog(e) {
    console.log(e);
    const dialogRef = this.dialog.open(e);
    dialogRef.afterClosed().subscribe(result => {
      console.log(`Dialog result: ${result}`);
      dialogRef == null
    });
  }

@Component({
  selector: 't1',
  templateUrl: 't1.html',
})
export class t1 {}

@Component({
  selector: 't2',
  templateUrl: 't2.html',
})
export class t2 {}

but once I remove the value and fix dialogRef.open, it works fine

const dialogRef = this.dialog.open(t1);
faisaljanjua
  • 886
  • 2
  • 13
  • 28

4 Answers4

18

This works for me. You can refer to this link too. link

You can use dialogRef.componentInstance.myProperty = 'some data' to set the data on your component.

You may need something like this:

let dialogRef = this.dialog.open(DialogComponent, {
            disableClose: true,
        });
dialogRef.componentInstance.name = 'Sunil';

Then in your DialogComponent you need to add your name property:

public name: string;
Yisi Tan
  • 309
  • 2
  • 5
2

Try something like this

constructor(
    public dialogRef: MatDialogRef<Component>,
    private dialog: MatDialog,
  ) { }


  openDialog(t1) {
    const dialogRef = this.dialog.open(NameComponent, {
      data: { t1 }
    });
    dialogRef.afterClosed().subscribe(data => {

  }

while retrieving in dialog component

 @Inject(MAT_DIALOG_DATA) public data: any,

Hope it works

Anil Kumar Reddy A
  • 665
  • 11
  • 29
0

You should send a variable instead of string. it should be t1 instead of 't1'

  <button mat-button (click)="openDialog(t1)">T1</button>
  <button mat-button (click)="openDialog(t2)">T2</button>

In your component.ts, you should declare the components

public t1 = new t1();
public t2 = new t2();

or you can try the below method using template variables

  <t1 #test1></t1>
  <t2 #test2></t2>
  <button mat-button (click)="openDialog(test1)">T1</button>
  <button mat-button (click)="openDialog(test2)">T2</button>
Nithya Rajan
  • 4,722
  • 19
  • 30
0
openDialog(dialog : string) {
    if(dialog == "t1"){
     const dialogRef = this.dialog.open(t1,{ data: { "YOURVALUE" }});
     dialogRef.afterClosed().subscribe(result => { this.YOURVALUE = result; });
    }
    else {
     const dialogRef = this.dialog.open(t2,{ data: { "YOURVALUE" }});
     dialogRef.afterClosed().subscribe(result => { this.YOURVALUE = result; });
    }
}
jcab
  • 1