Javascript 将JSON对象中的值设置为嵌套的formgroup
我确实有来自后端的JSON对象,并希望以更好的方式为其设置值。我应该考虑的最佳方法是什么? 我知道根据key参数手动设置对象的值 有没有其他优化的方法可以做到这一点Javascript 将JSON对象中的值设置为嵌套的formgroup,javascript,angular,typescript,reactive-forms,Javascript,Angular,Typescript,Reactive Forms,我确实有来自后端的JSON对象,并希望以更好的方式为其设置值。我应该考虑的最佳方法是什么? 我知道根据key参数手动设置对象的值 有没有其他优化的方法可以做到这一点 //后端响应 “答复”:{ “发送代码”:“123”, “senderName”:“test2”, “接管人代码”:“456”, “名称”:“测试”, “联系人号码”:“1233333333” } //formgroup结构 this.Form=this.fb.group({ 接收方公司代码:空, //发送者信息 发件人信息:th
//后端响应
“答复”:{
“发送代码”:“123”,
“senderName”:“test2”,
“接管人代码”:“456”,
“名称”:“测试”,
“联系人号码”:“1233333333”
}
//formgroup结构
this.Form=this.fb.group({
接收方公司代码:空,
//发送者信息
发件人信息:this.fb.group({
发件人公司代码:[null],
发件人名称:[null],
}),
//联系方式
联系人信息:this.fb.group({
名称:[空,验证程序。必需],
电话:[空,验证器。必填],
}),
});代码>这对你有帮助吗
const response = {
name: 'test',
telephone: '1233333333'
};
this.contactInformation.setValue(response);
这对你有帮助吗
const response = {
name: 'test',
telephone: '1233333333'
};
this.contactInformation.setValue(response);
您必须手动将数据映射到表单值
const mappedValue = {
receiverCompanyCode: response.receiverCompanyCode,
senderInformation: {
senderCompanyCode: response.senderCompanyCode,
senderName: response.senderName,
},
contactInformation: {
name: response.name,
telephone: response.telephone,
}
}
this.Form.patchValue(mappedValue);
或者直接更改FormGroup结构和补丁响应值
this.Form = this.fb.group({
senderCode: [null],
senderName: [null],
receiverCode: [null],
name: [null],
contactnumber: [null]
});
this.Form.patchValue(response);
您必须手动将数据映射到表单值
const mappedValue = {
receiverCompanyCode: response.receiverCompanyCode,
senderInformation: {
senderCompanyCode: response.senderCompanyCode,
senderName: response.senderName,
},
contactInformation: {
name: response.name,
telephone: response.telephone,
}
}
this.Form.patchValue(mappedValue);
或者直接更改FormGroup结构和补丁响应值
this.Form = this.fb.group({
senderCode: [null],
senderName: [null],
receiverCode: [null],
name: [null],
contactnumber: [null]
});
this.Form.patchValue(response);