Search code examples
angularangular-reactive-forms

Angular Reactive Form


I am new to Angular 5. I am currently working on Angular Reactive form. I have a below JSON structure that I need to post back to REST API after getting values from FORM.

JSON structure:

 {
  "empDesg": "Sr Developer",
  "empID": 123,
  "empName": "Sam",
  "empSkills": [
                "Java",
                "Devops"
               ]
 }

I managed to map empID, empName and empDesg to formcontrols; they all going to be input text elements. I wanna map empSkills to Checkboxes using formcontrols or formarray (but am not sure which one to use)- Here only I am stuck.

My HTML and component class:

addEmp.component.html

 <form class="form-emp" [formGroup]="empForm">
             <div class="form-group row ">
                <label for="empID" class="col-sm-2 col-form-label">EmpID</label>
                <div class="col-sm-10">
                    <input type="text" formControlName="empID" name="empID" class="form-control" id="empID" placeholder="Employee ID">
                     <div *ngIf="empForm.controls.empID.invalid && empForm.controls.empID.touched">
                    <ngb-alert type="danger" [dismissible]="false">Employee ID is must</ngb-alert>
                </div>
                </div>

                </div>
                <div class="form-group row">
                    <label for="empName" class="col-sm-2 col-form-label">EmpName</label>
                    <div class="col-sm-10">
                        <input type="text" formControlName="empName" name="empName" class="form-control" id="empName" placeholder="Employee Name">
                    </div>
                </div>
                <div class="form-group row">
                    <label for="empDesgn" class="col-sm-2 col-form-label">Emp Title</label>
                    <div class="col-sm-10">
                        <input type="text" formControlName="empDesg" name="empDesg" class="form-control" id="empDesgn" placeholder="Employee Title">
                    </div>
                </div>
                <p>Form value: {{ empForm.value | json }}</p>
                <div class="form-group row">
                    <div class="col-sm-2">Skillset</div>
                    <div class="col-sm-10">
                        <div class="form-check">
                            <input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
                            <label class="form-check-label" for="gridCheck1">
                            Java
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
                            <label class="form-check-label" for="gridCheck1">
                            Dot Net
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
                            <label class="form-check-label" for="gridCheck1">
                            Dev Ops
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
                            <label class="form-check-label" for="gridCheck1">
                            Business Analyst
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
                            <label class="form-check-label" for="gridCheck1">
                            Automation Testing
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" formControlName="skillSet" type="checkbox" id="gridCheck1">
                            <label class="form-check-label" for="gridCheck1">
                            UX
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-10">
                        <button type="submit" class="btn btn-primary" (click)="diag()">Add Employee</button>
                    </div>
                </div>
        </form>

addEmpComponent.ts

import { Component, OnInit } from '@angular/core';
import { Employee } from '../Employee'
import { FormBuilder, FormGroup, FormArray, FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-addemployee',
  templateUrl: './addemployee.component.html',
  styleUrls: ['./addemployee.component.css']
})
export class AddemployeeComponent implements OnInit {

  empForm: FormGroup;
  constructor(private fb: FormBuilder) {
    this.createForm();
  }

  createForm() {
    this.empForm = this.fb.group({
      empID: ['', Validators.required],
      empName: ['', Validators.required],
      empDesg: ['', Validators.required]      
    });    
  }

  model = new Employee();

  ngOnInit() {

  }  

}

Could you please share your inputs on how to map Checkboxes with skillSet array in the JSON structure I have above. If you share me the piece of code that would be helpful. Thanks a lot.


Solution

  • You can check about Reactive form in an app which I have created to play around with it. This app includes other features as well which you’ll surely come across sooner or later.

    skillList: any[] = ['Java',Dot Net','Dev Ops'];
    
    createForm() {
        this.empForm = this.fb.group({
          empID: ['', Validators.required],
          empName: ['', Validators.required],
          empDesg: ['', Validators.required],
          skills: this.fb.array(['Java','Devops']),        
        });    
      }
    
    isSkillChecked(data) {
       return this.rForm.controls['skills'].value.includes(data);
    }
    

    html

    <label>Skill Set:
          <span *ngFor="let skill of skillList">
            <input type="checkbox"
              (change)="onSkillChange(skill,$event.target.checked)"
              [checked]="isSkillChecked(skill)" /> {{skill}}
          </span>
    </label>
    

    I have modified my example to suit your needs. Take a look and let me know.