angular - Angular Reactive Forms,patchValue 不起作用,嵌套表单
问题描述
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);
}
}
解决方案
this.proposal
是异步获取的。任何直接依赖它的东西(比如你的patchValue
电话)都必须在订阅中才能使用它的价值。目前,该变量this.proposal
要么未分配,要么在patchValue
尝试时保持旧值。
尝试以下
ngOnInit() {
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.proposalService.getProposal().subscribe({ // <-- call here
next: (proposal: Proposal) => {
this.worksheetForm.patchValue({ // <-- patch the values here
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
}
});
},
error: err => console.log(err),
complete: () => console.log("successfully retrieved proposal data")
});
}
您可以在此处找到有关异步调用的更多信息。
推荐阅读
- sql-server - Windows 用户拒绝 SQL Server 批量 openrowset 访问,但拒绝 sa
- angular - 角度路由 - 需要一个在每次 url 更改时触发的 observable
- javascript - 重组代码以调用每个元素的 requestanimationframe
- npm - npm 错误!无法认证,需要:Basic realm="Artifactory Realm"
- c# - 如何扫描可用的移动设备并仅列出具有 DCIM 文件夹的移动设备?
- javascript - Web 组装速度与 js 等效
- javascript - 使用 Javascript 对对象中的扑克手进行排序
- javascript - Bootstrap 5 验证提交但不显示无效字段提示
- nuget - Nuget验证不适用于自签名证书
- ios - 如何从 FileManager 中删除文件?