passing parameter to mat-dialog open method
10,231
Solution 1
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;
Solution 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
Related videos on Youtube
Author by
faisaljanjua
Updated on June 04, 2022Comments
-
faisaljanjua almost 2 years
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 inentryComponent
. 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 fineconst dialogRef = this.dialog.open(t1);
-
Abhishek about 5 yearsif
t1
is your component add this in appModule or supporting Module file inentryComponents: []
array
-
-
Nithya Rajan about 5 yearsdo you have that instance of t1 in your component.ts
-
Nithya Rajan about 5 yearsi have updated my answer, you should get reference to your compnent class name in a property
-
faisaljanjua about 5 yearsafter declaring t1,t2, getttin this error
No component factory found for [object Object]. Did you add it to @NgModule.entryComponents?
-
Nithya Rajan about 5 yearsyou can try another method i have suggested
-
faisaljanjua about 5 yearsLet us continue this discussion in chat.
-
faisaljanjua about 5 yearsI though about this, but if in case of increase more buttons then like this
<button mat-button (click)="openDialog('t1')">T1</button> <button mat-button (click)="openDialog('t2')">T2</button> <button mat-button (click)="openDialog('t3')">T3</button> <button mat-button (click)="openDialog('t4')">T4</button>