Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use formControlName and deal with nested formGroup?

As Angular documentation says we can use formControlName in our forms:

<h2>Hero Detail</h2> <h3><i>FormControl in a FormGroup</i></h3> <form [formGroup]="heroForm" novalidate>     <div class="form-group">         <label class="center-block">Name:             <input class="form-control" formControlName="name">         </label>     </div> </form> 

As they say...

Without a parent FormGroup, [formControl]="name" worked earlier because that directive can stand alone, that is, it works without being in a FormGroup. With a parent FormGroup, the name input needs the syntax formControlName=name in order to be associated with the correct FormControl in the class. This syntax tells Angular to look for the parent FormGroup, in this case heroForm, and then inside that group to look for a FormControl called name.

Anyway some months ago I asked this to figure out what is the difference between formControlName and [formControl].

Now my question is: what about use formControlName with nested FormGroups?

For example, if I have the following form structure:

this.myForm = fb.group({     'fullname': ['', Validators.required],     'gender': [],     'address': fb.group({         'street': [''],         'houseNumber': [''],         'postalCode': ['']     }) }); 

What is the right way to bind "street" (or "houseNumber" or "postalCode") to related HTML elements using formControlName?

like image 863
smartmouse Avatar asked Jun 12 '17 12:06

smartmouse


People also ask

Can I use formControlName without FormGroup?

Without a parent FormGroup, [formControl]="name" worked earlier because that directive can stand alone, that is, it works without being in a FormGroup. With a parent FormGroup, the name input needs the syntax formControlName=name in order to be associated with the correct FormControl in the class.

How do I use FormControl inside FormGroup?

How to add a FormControl to a FormGroup. To add, update, or remove controls in FormGroup , use the following commands: addControl() adds a control and updates its value and validity. removeControl() removes a control.

Can we use formControlName in Div?

<div> is a readonly element, usually there's no point in binding form controls to it. You can also create your own custom components (or directives) that can be bound to form controls—if they implement ControlValueAccessor interface.


2 Answers

you can use Form group which is basically a collection of controls ( controls mean the fields given in your html form) define in your typescript syntax and binded to your HTML elements using the formControlName directive ,for example

this.myForm = fb.group({     'fullname': ['', Validators.required],     'gender': [],     'address': fb.group({         'street': [''],         'houseNumber': [''],         'postalCode': ['']     }) }); 

Template:

<form [formGroup]="myForm" >    <div class="form-group">       <label for="fullname">Username</label>       <input type="text" id="username" formControlName="fullname" class="form-control">                </div>    <div class="radio" *ngFor="let gender of genders">       <label>       <input type="radio" formControlName="gender" [value]="gender">{{ gender }} </label>    </div>    <div formGroupName="address">       <div class="form-group">          <label for="street">Username</label>          <input type="text" id="username" value="street" formControlName="street" class="form-control">                   </div>       <div class="form-group">          <label for="houseNumber">Username</label>          <input type="text" id="username" value="street" formControlName="houseNumber" class="form-control">                   </div>       <div class="form-group">          <label for="postalCode">Username</label>          <input type="text" id="username" value="street" formControlName="postalCode" class="form-control">                   </div>    </div> </form> 

A formGroup can consist of a nested formGroup and hierarchy can continue on ,but in accessing the value the its fairly simple .

like image 129
Somdatt_Bhadvariya Avatar answered Oct 14 '22 23:10

Somdatt_Bhadvariya


It is true. Look at formGroupName

this.myForm = fb.group({     'fullname': ['', Validators.required],     'gender': [],     'address': fb.group({         'street': [''],         'houseNumber': [''],         'postalCode': ['']     }) });  <form [formGroup]="myForm" >    <div class="form-group">       <label for="fullname">Username</label>       <input type="text" id="username" formControlName="fullname" class="form-control">                </div>    <div class="radio" *ngFor="let gender of genders">       <label>       <input type="radio" formControlName="gender" [value]="gender">{{ gender }} </label>    </div>    <div formGroupName="address">       <div class="form-group">          <label for="street">Username</label>          <input type="text" id="username" value="street" formControlName="street" class="form-control">                   </div>       <div class="form-group">          <label for="houseNumber">Username</label>          <input type="text" id="username" value="street" formControlName="houseNumber" class="form-control">                   </div>       <div class="form-group">          <label for="postalCode">Username</label>          <input type="text" id="username" value="street" formControlName="postalCode" class="form-control">                   </div>    </div> </form> 
like image 41
Albert Nizamutdinov Avatar answered Oct 14 '22 22:10

Albert Nizamutdinov