Angular 响应式表单,patchValue 不工作,嵌套表单
Angular Reactive Forms, patchValue not working, nested forms
Angular, 反应形式, Material 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")
});
}
您可以找到有关异步调用的更多信息here。
Angular, 反应形式, Material 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")
});
}
您可以找到有关异步调用的更多信息here。