I'm struggling to figure out how to convert an undefined object into a defined one in Angular TS. I have a table that allows for content modification using a modal window. The data from the row fills the fields of the modal window, and once changes are made, the returned object is of type Pacient:
constructor(
public dialogRef: MatDialogRef<pacientModifier>,
@Inject(MAT_DIALOG_DATA) public data: PacientModif) {
this.data={...data};
}
onNoClick(): void {
this.dialogRef.close({data:this.data});
}
However, when using dialogRef, the returned result is of an undefined type.
dialogRef.afterClosed().subscribe(result => {
this.updateRowData(result);
});
The returned result is an undefined object with members of the Pacient object.
The Pacient object:
export class Pacient {
id = "";
name = " ";
pacient_Condition =" ";
birth_Date=" ";
}
The object returned after closing the modal window:
data: Object { id: " ", name: " ", pacient_Condition: " ", birth_Date:"" }
I have attempted to cast using result as Pacient or using Object.values(result).
Patient's page template:
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>Actions</mat-header-cell>
<mat-cell *matCellDef="let element">
<button md-button (click)="openDialog(element)">Edit</button>
</mat-cell>
</ng-container>
Open dialog method:
openDialog(row:Pacient): void {
const dialogRef = this._dialog.open(pacientModifier, {
width: '250px',
data: row
});
dialogRef.afterClosed().subscribe(result => {
console.log(result);
this.updateRowData(result);
});
}
Is there a way to access the data member by member (Id, name, etc.) or convert it directly into a Pacient object?