如何在 angular2 material 的对话框中传递对象?
How to pass objects in angular2 material's dialog?
我的对话框包含一个填充对象的表单 "someresult" 我希望在用户单击对话框上的 "OK" 后将该对象传回应用程序组件。我该怎么做?
我在这个 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
值。 ?
安全导航在 undefined
或 null
时忽略 key
。
已编辑 demo 您的 Plunker。
我的对话框包含一个填充对象的表单 "someresult" 我希望在用户单击对话框上的 "OK" 后将该对象传回应用程序组件。我该怎么做?
我在这个 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
值。 ?
安全导航在 undefined
或 null
时忽略 key
。
已编辑 demo 您的 Plunker。