Reactive Forms
- import { FormGroup, FormArray , FormControl, FormBuilder } from '@angular/forms';
For example (Degrees)
- Raj – BCA
- Annu – B.Tech, MS
Therefore, we need an additional field as per the qualification and experience. In this demo, I’m creating additional fields for education and other fields by clicking on “Add Other Details”. This is a continuation of my last blog on Angular version 7.
- ngOnInit() {
- this.personalForm = this.formBuilder.group({
- firstName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(8)]],
- lastName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(8)]],
- address: ['', [Validators.required]],
- other: this.formBuilder.array([ this.addOtherSkillFormGroup()])
- });
- }
- addOtherSkillFormGroup(): FormGroup {
- return this.formBuilder.group({
- education: ['', Validators.required],
- age : ['', Validators.required],
- degree: ['Bachelor', Validators.required]
- });
- }
- addButtonClick(): void {
- (<FormArray>this.personalForm.get('other')).push(this.addOtherSkillFormGroup());
- }
- <div style="background-color: gainsboro">
- <div class="form-group">
- <div class="col-md-offset-2 col-md-4">
- <button type="button" class="btn btn-primary" (click)="addButtonClick()">
- Add Other Details
- </button>
- </div>
- </div>
- <div formArrayName="other" *ngFor="let other of personalForm.get('other').controls; let i = index" class="form-group">
- <div [formGroupName]="i">
- <div class="form-group" class="row cols-md-12">
- <label class="col-sm-2 control-label" for="education">
- Other
- </label>
- <div class="col-sm-4">
- <input type="text" class="form-control" id="education" placeholder="Education" formControlName="education">
- </div>
- <div class="col-sm-4">
- <input type="text" placeholder="Age" class="form-control" formControlName="age">
- </div>
- </div>
- <br>
- <div class="form-group" class="row cols-md-12">
- <label class="col-md-2 control-label">Degree Type</label>
- <div class="col-md-8">
- <label class="radio-inline">
- <input id="degree" type="radio" value="Bachelor" formControlName="degree">Bachelor
- </label>
- <label class="radio-inline">
- <input id="degree" type="radio" value="Master" formControlName="degree">Master
- </label>
- </div>
- </div>
- </div>
- </div>
- </div>

Prabodh BansalPosted Sep 13, 2019, 2:07 AM
Sir i need to talk to you . mu mobile no is 9810795067
Aress IndiaPosted Aug 12, 2019, 2:28 AM
How to remove dynamically added fields? and How to use setValidators to validate dynamic fields conditionally?
Rahul ShuklaPosted Apr 28, 2019, 4:13 AM
How to submit this form with details entry?
Amit Kumar SinghPosted Nov 15, 2018, 12:24 AM
Thanks for Sharing Rajendra !!!