如何在angular2材质的对话框中传递对象?

时间:2017-07-04 19:04:46

标签: angular angular-material2

我的对话框包含一个填充对象“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”时,我得到一个未定义的...

1 个答案:

答案 0 :(得分:-1)

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

编辑了你的Plunker demo