Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested form groups in angular 4

Tags:

angular

I am having the form group like

this.PatientInfo = this.fb.group({
      PatientID: [0],
      Gender: '',      
      Name:'',
      Employer: this.fb.group({
        EmployerID: 0,
        Name: [''],
        EmployerAddresses: this.fb.group({
          Address1: [''],
          Address2: [''],
          CityName: [''],          
          District: [''],
          StateName: [''],          
          PostalCode: [''],
          Country: ['']
        })
      })
    });

And i am binding the data like

<div class="col-md-3" formGroupName="EmployerAddresses">
    <div class="form-group form-group-default">
        <label class="control-label">State</label>
        <div class="input-group">
            <input type="text" class="form-control" formControlName="StateCode">
        </div>
    </div>
 </div>

On top i kept the formgroup as PatientInfo.

Here i am getting the error "cannot find the control with name EmployerAddresses".

Can you please anyone help on this. Thanks in advance.

like image 440
Sivakishore Teru Avatar asked Nov 26 '17 06:11

Sivakishore Teru


People also ask

Can we have nested forms in Angular?

Nested Forms will allow us to create the array of objects for a single field and this hierarchy goes on. (i.e) A single object can have a number of objects inside of it, and we can achieve it in Reactive forms through the concept of “Form Array”.

What are form groups in Angular?

A FormGroup aggregates the values of each child FormControl into one object, with each control name as the key. It calculates its status by reducing the status values of its children. For example, if one of the controls in a group is invalid, the entire group becomes invalid.

What is the difference between FormGroup and FormGroupName?

FormGroupDirective is a directive that binds a FormGroup to a DOM element. FormGroupName is a directive that links a nested FormGroup to a DOM element.

What is Controlvalueaccessor in Angular?

Defines an interface that acts as a bridge between the Angular forms API and a native element in the DOM.


1 Answers

This code solved my issue.

this.PatientInfo = this.fb.group({
  PatientID: [0],
  Gender: '',      
  Name:'',
  Employer: this.fb.group({
    EmployerID: 0,
    Name: [''],
    EmployerAddresses: this.fb.group({
      Address1: [''],
      Address2: [''],
      CityName: [''],          
      District: [''],
      StateName: [''], 
      StateCode: [''],
      PostalCode: [''],
      Country: ['']
    })
  })
});

<div class="col-md-3" formGroupName="Employer">
  <div class="form-group form-group-default" formGroupName="EmployerAddresses">
      <label class="control-label">State</label>
      <div class="input-group">
          <input type="text" class="form-control" formControlName="StateCode">
      </div>
  </div>
</div>

Thank you...

like image 122
Sivakishore Teru Avatar answered Oct 09 '22 10:10

Sivakishore Teru