我用了dialog box个角material .

我想把数据传递给打开的组件.这里是我如何打开一个按钮点击对话框

let dialogRef = this.dialog.open(DialogComponent, {
            disableClose: true,
            data :{'name':'Sunil'}
        });

在文档页面上有data属性,但我在安装的软件包中判断了MdDialogConfig

/**
 * Configuration for opening a modal dialog with the MdDialog service.
 */
export declare class MdDialogConfig {
    viewContainerRef?: ViewContainerRef;
    /** The ARIA role of the dialog element. */
    role?: DialogRole;
    /** Whether the user can use escape or clicking outside to close a modal. */
    disableClose?: boolean;
    /** Width of the dialog. */
    width?: string;
    /** Height of the dialog. */
    height?: string;
    /** Position overrides. */
    position?: DialogPosition;
}

配置类中没有数据属性.

现在我如何访问传递的数据?

推荐答案

更新2(Angular 5+)

这个答案已经过时了.看看epiphanatic's answer instead.

使现代化

可以使用dialogRef.componentInstance.myProperty = 'some data'设置组件上的数据.

你需要这样的东西:

let dialogRef = this.dialog.open(DialogComponent, {
            disableClose: true,
        });
dialogRef.componentInstance.name = 'Sunil';

然后在你的DialogComponent中,你需要添加name property:

...

@Component({
  ...
})
export class DialogComponent {
   public name: string;

   ...

}

较新版本的@下角material 无效

I didn't find any documentation on this, so i started looking into the source code too. Because of that, this might not be the official way of to do.

我成功地将数据定位在dialogRef._containerInstance.dialogConfig.data

所以你能做的就是举个例子

let name = dialogRef._containerInstance.dialogConfig.data.name;
console.log(name); // Sunil

Typescript相关问答推荐

为什么AB和CD这两种类型在TypScript中可以相互分配?

如何在TypScript中使参数类型化但可选

类型脚本如何将嵌套的通用类型展开为父通用类型

如何在ts中使用函数重载推断参数类型

如何正确修复TypScript 4.7到4.8升级中的TS 2345编译错误

如何在排版中正确键入中间件链和控制器链

泛型类型,引用其具有不同类型的属性之一

在MessageStore对象中实现条件类型时出现TypeScrip错误

如何以Angular 形式显示验证错误消息

使用打字Angular 中的通用数据创建状态管理

如何调整对象 struct 复杂的脚本函数的泛型类型?

在Reaction-Native-ReAnimated中不存在Animated.Value

转换器不需要的类型交集

如何将对象数组中的键映射到另一种对象类型的键?

17个Angular 的延迟观察.如何在模板中转义@符号?

如何正确地将元组表格输入到对象数组实用函数(如ZIP)中,避免在TypeScrip 5.2.2中合并所有值

在TypeScript中,如何通过一系列过滤器缩小类型?

有没有什么方法可以使用ProvideState()提供多个削减器作为根减减器

如何以类型安全的方式指定键的常量列表,并从该列表派生所挑选的接口?

将对象数组传递给 Typescript 中的导入函数