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

标签 angular angular-material angular-material2

对于 mdDialog ,如何传入变量?具体来说,如何将 Angular 服务注入(inject)到对话框组件中?

最佳答案

为了传递变量,您可以从 MdDialog.open() 方法调用返回的 MdDialogRef 实例中获取在对话框中打开的组件的实例。

dialogRef = this.dialog.open(PizzaDialog, config)
dialogRef.componentInstance.<property_name>

来自 的改良披萨 github material2 docs angular material doc

@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>) { }
}

关于angular - 如何将服务变量传递到 Angular Material 对话框中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40648252/

相关文章:

Angular 类不工作

angular - Angular/RxJs-在失败的请求上访问响应头

javascript - Angularjs 在使用 http post 添加项目后获取数据,无需使用 mdDialog 刷新

angular - NullInjectorError : No provider for MediaMatcher

Angular Material matInput 内容右对齐

angular - 反转 mat-checkbox 的 "checked"值

angular - 将自定义元素和属性添加到编译器模式

c# - 浏览器OPTIONS请求方式和windows授权

angular-material - mat-form-field 必须包含一个 MatFormFieldControl。甚至我添加了 MatFormFieldModule

angular - Mat-tree 创建一棵树,其中包含要显示的值和 id