【发布时间】:2021-06-07 00:24:42
【问题描述】:
Angular、响应式表单、材质 UI,
我在拆分组件中设置了嵌套表单。我使用 InMemoryWebAPI 设置了一些虚假数据,尽管它们显示在上面的组件中,但其中一些属性显示为未定义。
patchValue 是显示从数据库检索到的初始值的正确方法吗?
export class WorksheetComponent implements OnInit {
worksheetForm: FormGroup;
memberInfoForm: FormGroup;
proposal: any;
constructor(
private fb: FormBuilder,
private proposalService: ProposalService
) {}
getProposal(): void {
this.proposalService.getProposal().subscribe((proposal: Proposal) => {
(this.proposal = proposal),
err => console.log(err),
() => console.log("successfully retrieved proposal data");
});
}
ngOnInit() {
this.getProposal();
this.memberInfoForm = this.fb.group({
firstName: ["", [Validators.required]],
lastName: ["", [Validators.required]],
address1: ["", [Validators.required]],
address2: ["", [Validators.required]],
city: ["", [Validators.required]],
state: ["", [Validators.required]],
zipCode: ["", [Validators.required, Validators.minLength(5)]],
phoneNumber: ["", [Validators.required]],
emailAddress: ["", [Validators.required, Validators.email]]
});
this.worksheetForm = this.fb.group({
memberInfoForm: this.memberInfoForm
});
this.worksheetForm.patchValue({
memberInfoForm: {
firstName: this.proposal.borrower.firstName,
lastName: this.proposal.borrower.lastName,
address1: this.proposal.borrower.address1,
address2: this.proposal.borrower.address2,
city: this.proposal.borrower.city,
state: this.proposal.borrower.state,
zipCode: this.proposal.borrower.zipCode,
phoneNumber: this.proposal.borrower.phoneNumber,
emailAddress: this.proposal.borrower.emailAddress
}
});
}
onSubmit() {
console.log(this.worksheetForm.value);
}
}
【问题讨论】:
标签: angular nested-forms angular10 reactive-forms