将输入值传递给Dialog Component

Sum*_*wal 11 angular2-material angular

我正在实现material2的对话框组件并面临这个问题:

我想为所有确认类型的消息创建一个通用对话框,开发人员可以根据业务需求将文本输入到对话框中.但根据文件,没有这样的规定.我们是否有相同的解决方法,或者我应该将其作为github上的功能请求发布?

export class ConfirmationDialogComponent implements OnInit {

@Input() confirmationText: string;
@Input() confirmationTitle: string;
@Input() confirmationActions: [string, string][] = [];

constructor(public dialogRef: MdDialogRef<ConfirmationDialogComponent>) {
}

ngOnInit() {
}

}
Run Code Online (Sandbox Code Playgroud)

这样打电话:

let dialogRef = this.dialog.open(ConfirmationDialogComponent);
Run Code Online (Sandbox Code Playgroud)

Mil*_*lad 21

open会给你组件实例,意味着你可以像这样注入你想要的东西:

openDialog() {
    let dialogRef = this.dialog.open(DialogOverviewExampleDialog);
    let instance = dialogRef.componentInstance;
    instance.text = "This text can be used inside DialogOverviewExampleDialog template ";
    console.log('dialogRef',dialogRef);
  }
Run Code Online (Sandbox Code Playgroud)

然后显然在DialogOverviewExampleDialog模板中你可以做到:

   this is the text {{text }}
Run Code Online (Sandbox Code Playgroud)

Plunker

我通常会做什么,我会创建一个我的Component理解的配置对象,然后在打开模态时我会传递它:

 private config = {
    title :"Hello there ",
    text :"What else ? "
 };

 openDialog() {
    let dialogRef = this.dialog.open(DialogOverviewExampleDialog);
    let instance = dialogRef.componentInstance;
    instance.config = this.config;
    console.log('dialogRef',dialogRef);
  }
Run Code Online (Sandbox Code Playgroud)

然后在模态组件内:

<div class="my-modal">
   <h1>{{config.title}}</h1>
   <p>{{config.text}}</p>
</div>
Run Code Online (Sandbox Code Playgroud)