【发布时间】: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”时,我得到一个未定义的...
【问题讨论】:
-
我能够访问
key值。看看这是否正确? plnkr.co/edit/XsQaUtcliGQpm928iDrw?p=preview -
有效,作为答案发布,我将标记为正确。