Angular6 forms automatically fill in data

  • template code description:
    Angular6 response form has two FormControl, indicating name and age:
    name: < input type= "text" formControlName= "name" > < / div >
    Age: < input type= "text" formControlName= "age" > < / div >
  • what I want to achieve:
    when you open the page where the form is located, the instance of an entity class in this page has been assigned.
    requires that when you open the page where the form is located, fill in the corresponding data in the instance
    for example:
    when you open the page where the form is located, instance = {name:" Zhang San", age:15}
    , then the two input are filled in turn: Zhang San, 15
  • .
  • ask for help:
    I don"t know if there is a clear description above, it is best to provide code, and if not, it is also good to provide ideas.
    Thank you very much!
Jun.18,2021

set the value of formGroup directly after the formGroup is instantiated or assigned.

instance = { name:'', age:15 }
formGroup.patchValue({
    name:instance.name,
    age:instance.age
})
Menu