2016-12-28 106 views
3

我实现材料2的对话框组件,面对这个问题:传递输入值对话框组件

我想打一个通用对话框所有确认类型的消息,凡开发商可以输入文本对话框根据业务需求。但根据docs没有这样的规定。我们是否有相同的解决方案,或者我应该在github上将它作为功能请求发布?

export class ConfirmationDialogComponent implements OnInit { 

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

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

ngOnInit() { 
} 

} 

调用是这样的:

let dialogRef = this.dialog.open(ConfirmationDialogComponent); 
+0

请发表您的代码 – yurzui

+0

@yurzui添加的代码 –

+1

检查http://stackoverflow.com/questions/34205593/working-example-of-angular-2-0-material-mddialog-with-angular - 2-0/40185852#40185852请参阅步骤8 – yurzui

回答

10

开放会给你的组件实例,意味着你可以注入你什么都想要它像这样:

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); 
    } 

那么显然里面的DialogOverviewExampleDialog模板你可以做:

this is the text {{text }} 

Plunker

我通常会做什么,我会创造出我的组件能够理解的配置对象,然后打开该模式时,我会通过这一点:

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); 
    } 

然后模态组件内部:

<div class="my-modal"> 
    <h1>{{config.title}}</h1> 
    <p>{{config.text}}</p> 
</div> 
+0

谢谢你。我需要在初始化组件之前传递参数。还有另一个线程,https://stackoverflow.com/questions/40648252/how-can-i-pass-a-service-variable-into-an-angular-material-dialog –