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

eth*_*han 39 angular-material angular-material2 angular

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

poi*_*dar 58

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

dialogRef = this.dialog.open(PizzaDialog, config)
dialogRef.componentInstance.<property_name>
Run Code Online (Sandbox Code Playgroud)

来自github material2 docs的 Modified Pizza

@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>) { }
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为ThiagoPXP(MdDialogConfig数据对象)的答案非常好,是将数据传递到子组件然后在构造函数中设置子类变量的更好方法,见下文 (2认同)

Thi*_*PXP 39

Material2 beta.2

dialog.open()函数采用第二个参数config(MdDialogConfig),您可以在其中指定任何data对象.

this.dialog.open(YourComponent, {
  data: {
    anyProperty: "myValue"
  }
});
Run Code Online (Sandbox Code Playgroud)

然后,您可以从用于对话框窗口的组件中检索此对象.

export class YourDialogComponent {

  constructor(public dialogRef: MdDialogRef<YourComponent>) {
    console.log('data', this.dialogRef.config.data);
  }
}
Run Code Online (Sandbox Code Playgroud)

更新:beta.3

上面的答案适用于2.0.0-beta.2Material2 的版本.如果您正在使用2.0.0-beta.3,则该config属性已被删除MdDialogRef.您可以使用MD_DIALOG_DATA打开的组件注入该值.

新的进口报表

import {MdDialog, MdDialogRef, MdDialogConfig, MD_DIALOG_DATA} from '@angular/material';
Run Code Online (Sandbox Code Playgroud)

开放式对话

this.dialog.open(YourComponent, {
  data: {
    anyProperty: "myValue"
  }
});
Run Code Online (Sandbox Code Playgroud)

DialogRef组件中检索数据

export class YourDialogComponent {

  constructor(
    public dialogRef: MdDialogRef<YourDialogComponent>,
    @Inject(MD_DIALOG_DATA) public data: any) {

    console.log('data', this.data);
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,这是关于"关注点分离".打开对话框的组件不应该知道对话框如何在内部工作.因此,它不应该直接设置任何对话框的属性.更可预见的是,对话框可以在其构造函数中启动所有必需的数据. (3认同)

Fla*_*ken 9

来自https://material.angular.io/components/dialog/overview上的官方文档

使用Dialog组件共享数据.

如果要与对话框共享数据,可以使用data选项将信息传递给对话框组件.

let dialogRef = dialog.open(YourDialog, {
  data: 'your data',
});
Run Code Online (Sandbox Code Playgroud)

要访问对话框组件中的数据,必须使用MD_DIALOG_DATA注入令牌:

import {Component, Inject} from '@angular/core';
import {MD_DIALOG_DATA} from '@angular/material';

@Component({
  selector: 'your-dialog',
  template: 'passed in {{ data }}',
})

export class YourDialog {
  constructor(@Inject(MD_DIALOG_DATA) public data: any) { }
}
Run Code Online (Sandbox Code Playgroud)


Wil*_*ire 6

要给出更新的答案以适应从“Md”到“Mat”的更新:

  • 这假设您已经成功实现了一个对话框,现在只是想添加一个输入
  • 当您遇到 @angular/material 没有导出成员“MD_DIALOG_DATA”的问题时,这是解决方案

要打开包含数据的对话框,请传入数据对象:

this.dialog.open(YourComponent, {
  data: {
    anyProperty: "myValue"
  }
});
Run Code Online (Sandbox Code Playgroud)

要在对话框中检索该数据:

import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';

export class YourDialogComponent {

  constructor(
    public dialogRef: MatDialogRef<YourDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any) {

    console.log('data passed in is:', this.data);
  }
}
Run Code Online (Sandbox Code Playgroud)


cam*_*kid 5

这就是我做到的.

pizza.service.ts

import { Injectable } from '@angular/core';

@Injectable()
export class PizzaService {
    getTopping(): string {
        return "Mushrooms"
    }
}
Run Code Online (Sandbox Code Playgroud)

pizzaDialog.component.ts

import { Component } from '@angular/core';
import { MdDialogRef} from '@angular/material';
import {PizzaService} from './pizza.service';

@Component({
    selector: 'pizza-dialog',
    template: `{{pizzaTopping}}
    <button type="button" (click)="dialogRef.close('yes')">Yes</button>
    <button type="button" (click)="dialogRef.close('no')">No</button>
  `,
    providers: [PizzaService]
})
export class PizzaDialog {
    pizzaTopping: string;

    constructor(public dialogRef: MdDialogRef<PizzaDialog>, private pizzaService: PizzaService) { };

    ngOnInit(): void {
        this.pizzaTopping = this.pizzaService.getTopping()
    }
}
Run Code Online (Sandbox Code Playgroud)