如何将服务变量传递到Angular Material对话框?

前端之家收集整理的这篇文章主要介绍了如何将服务变量传递到Angular Material对话框?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对于 mdDialog,我如何传入变量?具体来说,如何将Angular服务注入对话框组件?
对于传递变量,您可以从MdDialog.open()方法调用中返回的MdDialogRef实例中获取对话框中打开的组件的实例.
dialogRef = 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>) { }
}

猜你在找的Angularjs相关文章