【问题标题】:How to pass objects in angular2 material's dialog?如何在角度材质对话框中传递对象?
【发布时间】:2017-07-04 19:04:46
【问题描述】:

我的对话框包含一个填充对象“someresult”的表单我希望在用户单击对话框上的“确定”后将该对象传递回应用程序组件。我该怎么做?

我在这个 PLUNKR 中包含了一个简单的例子。 http://plnkr.co/edit/qjpY6m5C8bgJjRfm4Yot?p=preview

import { MdDialogRef } from '@angular/material';
import { Component } from '@angular/core';
@Component({
    selector: 'confirm-dialog',
    template: `
        <p>{{ title }}</p>
        <p>{{ message }}</p>

        <button type="button" md-raised-button 
            md-dialog-close="someresult">OK</button>

        <button type="button" md-button 
            (click)="dialogRef.close()">Cancel</button>
    `,
})

    export class ConfirmDialog {

        public title: string;
        public message: string;
        public someresult = { // this is the object I want to pass back
          'key': 'somevalue'
        }

        constructor(public dialogRef: MdDialogRef<ConfirmDialog>) {

        }
    }

在我的 app.component.ts 中,我希望能够使用 console.log 注销结果,但我似乎无法让对象正确注销...

  public openDialog() {
    this.dialogsService
      .confirm('Confirm Dialog', 'Are you sure you want to do this?')
      .subscribe(res => {this.result = res; console.log(res.key);});
  }

请注意,如果我记录了 console.log(key),我会在控制台中看到 [Object object],但是当我尝试访问键“key”时,我得到一个未定义的...

【问题讨论】:

标签: angular angular-material2


【解决方案1】:

我能够访问key 值。 ? 安全导航在 undefinednull 时忽略 key

编辑了您的 Plunker 的 demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2020-02-23
    • 2019-06-22
    • 2021-08-22
    相关资源
    最近更新 更多