对于
mdDialog,我如何传入变量?具体来说,如何将Angular服务注入对话框组件?
对于传递变量,您可以从MdDialog.open()方法调用中返回的MdDialogRef实例中获取对话框中打开的组件的实例.
原文链接:https://www.f2er.com/angularjs/143874.htmldialogRef = this.dialog.open(PizzaDialog,config) dialogRef.componentInstance.<property_name>
来自github material2 docs的改良比萨饼
@Component({ selector: 'pizza-component',template: ` <button type="button" (click)="openDialog()">Open dialog</button> ` }) export class PizzaComponent { constructor(public dialog: MdDialog) { } openDialog() { let config = new MdDialogConfig(); let dialogRef:MdDialogRef<PizzaDialog> = this.dialog.open(PizzaDialog,config); dialogRef.componentInstance.name = "Ham and Pineapple"; dialogRef.componentInstance.size = "Large"; } } @Component({ selector: 'pizza-dialog',template: ` <h2>{{name}}</h2> <p>Size: {{size}}</p> <button type="button" (click)="dialogRef.close('yes')">Yes</button> <button type="button" (click)="dialogRef.close('no')">No</button> ` }) export class PizzaDialog { name:string; size:string; constructor(public dialogRef: MdDialogRef<PizzaDialog>) { } }