将参数传递给 mat-dialog 打开方法

fai*_*jua 6 dialog angular-material angular

Angular 7.1AngularMaterial 7.3

我正在尝试调用函数并传递一些值,它提示以下错误

未找到 t1 的组件工厂。你把它添加到@NgModule.entryComponents 了吗?

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

  <button mat-button (click)="openDialog('t1')">T1</button>
  <button mat-button (click)="openDialog('t2')">T2</button>
Run Code Online (Sandbox Code Playgroud)

一旦我传递了值,它就会显示上面的代码。

  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 {}
Run Code Online (Sandbox Code Playgroud)

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

const dialogRef = this.dialog.open(t1);
Run Code Online (Sandbox Code Playgroud)

小智 7

这对我有用。你也可以参考这个链接。关联

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

你可能需要这样的东西:

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

然后在你的 DialogComponent 你需要添加你的 name 属性:

public name: string;
Run Code Online (Sandbox Code Playgroud)


Nit*_*jan 0

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

  <button mat-button (click)="openDialog(t1)">T1</button>
  <button mat-button (click)="openDialog(t2)">T2</button>
Run Code Online (Sandbox Code Playgroud)

在你的 component.ts 中,你应该声明组件

public t1 = new t1();
public t2 = new t2();
Run Code Online (Sandbox Code Playgroud)

或者您可以尝试使用以下方法template variables

  <t1 #test1></t1>
  <t2 #test2></t2>
  <button mat-button (click)="openDialog(test1)">T1</button>
  <button mat-button (click)="openDialog(test2)">T2</button>
Run Code Online (Sandbox Code Playgroud)