1

I want to patch a form with slightly different field name. Is this possible in Angular?
Example: suppose this is my student.ts class.

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. Is there any way I can also patch it?

2
  • problem is with exported class "Student". Make sure key "bivalueTerm" is the same as "BIValueTerm". Either use "bivalueTerm" in Student class or "BIValueTerm" as from element then only it will assign properly Commented Mar 17, 2020 at 7:08
  • or else you can use studentForm.patchValue({id: student.id, bivalueTerm: student. BIValueTerm}) Commented Mar 17, 2020 at 7:12

1 Answer 1

1

Two way to do get an appropriate result is solution 1:

export class Student {
   id: number;
   bivalueTerm: number;
}

Solution 2:

studentForm.patchValue(
  {
   id: student.id, 
   bivalueTerm: student.BIValueTerm
  }
)
Sign up to request clarification or add additional context in comments.

3 Comments

yes, both the approaches would work, but that doesn't answer my question. I want to know if I can patch it by changing Patch configuration such that the keys are matched case insensitively.
No to my knowledge, it is not possible.
I marked your answer as correct, as I found it no other way other than you mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.