【发布时间】:2020-05-17 08:48:46
【问题描述】:
我有一个简单的 Angular Material 对话框,它应该显示一些关于一个人的数据。我正在显示的人员对象如下:
export interface Person {
prsName: string;
prsAddress: string;
prsCity: string;
prsState: string;
}
对话框的 Typescript 代码如下:
@Component({
selector: 'app-persondialog',
templateUrl: './persondialog.component.html',
styleUrls: ['./persondialog.component.css']
})
export class PersonDialogComponent {
constructor(public dialogRef: MatDialogRef<PersonDialogComponent>,
@Inject(MAT_DIALOG_DATA) public theInfo: Person) {
alert('Name: '+theInfo.prsName); /* temporary call to check the contents of theInfo */
}
onClose(): void {
this.dialogRef.close();
}
从我的主要组件调用的打开对话框的函数如下:
public getPerson(aName: string) {
const found = this.persGetter.findPerson(aName);
found.subscribe(info => {
alert('Person Name: ' + info.prsName + ' Address: ' + info.prsAddress); /* Temporary. This just shows that info has real data! */
const ref = this.theDlg.open(PersonDialogComponent, {
width: '300px',
data: { theInfo: info }
});
});
}
注意,persGetter 是一个 HttpClient 对象,它从我的服务器获取 Person 信息。
对话框的 HTML 如下:
<h1 mat-dialog-title>Person: {{ theInfo.prsName }}</h1>
<div mat-dialog-actions>
<button mat-button (click)="onClose()">Close</button>
</div>
对话框在调用 getPerson() 函数时显示,但由于某种原因,在警报显示 info 内容和实际创建对话框之间,prsName 属性变为未定义!
显然我错过了一些东西。谁能告诉我如何将我的个人信息成功传递到此对话框中?
【问题讨论】:
标签: angular typescript angular-material