简体   繁体   中英

Angular Forms Patch Value With Different Field Name

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?

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
  }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM