Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
How add dynamic fields in angular form

I have to create a dynamic form where label and input fields will both are text-field(user will fill at run time). And user can add as many as fields he requires. I have requirements where I have to generate. property file in backend. Where key-value pair is required and the user decides how much key-value pair he needs.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Create FormGroup as

  keyValueFA = new FormArray([this.newKeyValueFG]);

  propertyFG = new FormGroup({
    keyValue: this.keyValueFA,
  });

getter to create new FormGroup

  get newKeyValueFG(): FormGroup {
    return new FormGroup({
      key: new FormControl(null),
      value: new FormControl(null),
    });
  }

Other methods to set, remove, get FormGroup

  get keyValueArrayFGControls(): FormGroup[] {
    return this.keyValueFA.controls as FormGroup[];
  }

  addNewKeyValueFG(): void {
    this.keyValueFA.push(this.newKeyValueFG);
  }

  removeNewKeyValueFG(index: number): void {
    this.keyValueFA.removeAt(index);
  }

In template iterate keyValueArrayFGControls

<form [formGroup]="propertyFG">
  <div
    formArrayName="keyValue"
    *ngFor="let fg of keyValueArrayFGControls; let i = index"
  >
    <div [formGroup]="fg">
      {{ i + 1 }})
      <div><input type="text" formControlName="key" placeholder="Key" /></div>
      <div>
        <input type="text" formControlName="value" placeholder="Value" />
      </div>
      <button (click)="removeNewKeyValueFG(i)">Remove</button>
    </div>
  </div>
  <button (click)="addNewKeyValueFG()">Add</button>
</form>

Working demo

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda