0

I am creating an Edit Employee form and there is a button Add new skill which need to create new text box to add the skill. The sample output

But when I click on Add new skill, it is not adding any new text boxes and displaying these details inside the console.

Console

My typescript code :

import { Component, OnInit } from '@angular/core';
import { FormArray, FormBuilder, NgForm, Validators } from '@angular/forms';
import { EmployeeDepartment } from '../employee-department-class';


@Component({
  selector: 'app-edit-emp-form-builder',
  templateUrl: './edit-emp-form-builder.component.html',
  styleUrls: ['./edit-emp-form-builder.component.css']
})
export class EditEmpFormBuilderComponent implements OnInit {

  /**
   * Using some default values and may be different from other component
   */


  department = [
    new EmployeeDepartment(1, 'Payroll'),
    new EmployeeDepartment(2, 'Internal'),
    new EmployeeDepartment(3, 'HR')
  ]

  skills: any = []
  employeeDetails = this.fb.group({
    employeeName: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(20)]],
    department: [null, [Validators.required]],
    skills: this.fb.array([
      this.fb.control('')
    ])
  });
  constructor(private fb: FormBuilder) {

  }

  get skill() {
    return this.employeeDetails.get('skills') as FormArray;
  }

  addSkill() {
    this.skills.push(this.fb.control(''));
  }

  onSubmit(f: NgForm) {
    console.log(f);
  }

  ngOnInit(): void {
  }

}

My HTML code for the skill

 <div formArrayName="skills">
    <h3>Skill</h3>
    <button (click)="addSkill()">Add new skill</button>
    <div *ngFor="let s of skill.controls; let i = index">
      <label>Skill:
        <input type="text" [formControlName]="i">
      </label>
    </div>

  </div>
2
  • follow this blog : medium.com/aubergine-solutions/… Commented May 21, 2021 at 4:57
  • you has a type error is this.skill.push(this.fb.control(''));, you wrote skills -ends in "s"- (the "this.skill is the "getter" you has) Commented May 21, 2021 at 6:46

1 Answer 1

1

Remove the variable skills: any = [] from your code.
Rename get skill() to get skills()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.