[英]Angular Forms Patch Value With Different Field Name
I want to patch a form with slightly different field name.我想修补一个字段名称略有不同的表单。 Is this possible in Angular?
这在 Angular 中可能吗?
Example: suppose this is my student.ts
class.示例:假设这是我的
student.ts
类。
export class Student {
id: number;
BIValueTerm: number;
}
I want to patch my formgroup with a student object.我想用学生对象修补我的表单组。 my formgroup looks like this:
我的表单组如下所示:
// const student = ... ; student object
const studentForm = new FormGroup({
id: new FormControl(''),
bivalueTerm: new FormControl('')
});
studentForm.patchValue(student);
now the problem is, studentForm
properly patches the id
field but not bivalueTerm
field.现在的问题是,
studentForm
正确地修补了id
字段而不是bivalueTerm
字段。 Is there any way I can also patch it?有什么办法我也可以修补它吗?
Two way to do get an appropriate result is solution 1:获得适当结果的两种方法是解决方案1:
export class Student {
id: number;
bivalueTerm: number;
}
Solution 2:解决方案2:
studentForm.patchValue(
{
id: student.id,
bivalueTerm: student.BIValueTerm
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.