Example
,但我不知道如何正确地将数据传递到mat对话框。这是我的例子。首先,我有一个发出put请求的服务和另一个发布数据的服务:
editUser(employee: Employees) {
return this.http.put(this._url + '/' + employee.id, httpOptions).pipe(
map((res: Response) => res.json)
)
}
saveUser(employee: Employees) {
return this.http.post(this._url, employee, httpOptions).subscribe((res: any) => {
res = employee
this.componentMethodCallSource.next();
})
}
然后在我的
table.component
同学们,我有按钮的功能。这将选择行,调用服务,并打开对话框。
editUser() {
const selectRows = this.gridApi.getSelectedRows();
const editSub = selectRows.map((rowToEdit) => {
return this.service.editUser(rowToEdit);
});
this.openDialog(FormComponent)
}
ngoninit
. 这个条件决定了我是否
loadUser
MAT_DIALOG_DATA
加载json id,该id将确定
loadUsers()
应该被叫来。但我似乎无法访问我的id。我一直有一个错误,说它是
undefined
窗体组件
employeeInfo: Employees;
userId: number;
constructor(
public dialogRef: MatDialogRef,
private fb: FormBuilder,
private service: Service,
private http: HttpClient,
@Inject(MAT_DIALOG_DATA) public data: any
) {
this.userId = this.data.userId;
}
ngOnInit() {
this.createUserForm();
if (this.userId > 0) {
this.loadUser();
}
}
createUserForm() {
this.frmUser = this.fb.group({
id: [],
firstName: ["", [Validators.required]],
lastName: ["", Validators.required],
})
}
onSave() {
this.employeeInfo = this.frmUser.getRawValue();
this.employeeInfo.id = this.userId;
this.service.saveUser(this.employeeInfo)
this.dialogRef.close({
success: 'success',
data: {
emp: this.employeeInfo.id
}
})
}
loadUser() {
this.service.getData(this._url).subscribe((res: any) => {
this.frmUser.patchValue({
firstName: res.FirstName,
})
})
}
编辑:更新代码。仍无法将数据发送到表单:
表格组件:
editUser(user) {
const dialogRef = this.dialog.open(FormComponent, {
width: '250px',
data: user
});
dialogRef.afterClosed().subscribe(result => {
this.user = user;
});
}
constructor( public dialogRef: MatDialogRef,
private fb: FormBuilder, private service: Service, private http: HttpClient, @Inject(MAT_DIALOG_DATA) public data: Employees) {
}