将参数传递给MAT-DIALOG打开方法



angular 7.1 AngularMaterial 7.3

我试图调用函数并传递一些值,它提示后遵循错误

找不到T1的组件工厂。您是否将其添加到 @ngmodule.entrycomponents?

尽管t1包含在entryComponent中。但是,一旦删除传递值以修复值,它将起作用。

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

我将其通过alture显示上述代码。

  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 {}

但是,一旦我删除该值并修复了dialogRef.open,它就可以正常工作

const dialogRef = this.dialog.open(t1);

这对我有用。您也可以参考此链接。链接

您可以使用dialogRef.componentInstance.myProperty = 'some data'在组件上设置数据。

您可能需要这样的东西:

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

然后在您的DialogComponent中添加您的名称属性:

public name: string;

尝试类似的东西

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

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

在对话框组件中检索时

 @Inject(MAT_DIALOG_DATA) public data: any,

希望它有效

您应该发送一个变量而不是字符串。它应该是t1而不是't1'

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

在您的组件中,您应该声明组件

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

或者您可以使用template variables

尝试以下方法
  <t1 #test1></t1>
  <t2 #test2></t2>
  <button mat-button (click)="openDialog(test1)">T1</button>
  <button mat-button (click)="openDialog(test2)">T2</button>
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; });
    }
}

最新更新