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

517
Vistas
How to take value from input which is in *ngFor loop

The problem is that we have to take values that are under *ngFor loop so, it's taking value that is coming from the first input. We have to sore value in an array that is coming from the value of inputs.

HTML

<div *ngFor="let ele of container">
  <div *ngIf="keyValue === ''">
    <input type="text" [(ngModel)]="keyValue" />
    <textarea
      name=""
      id=""
      cols="30"
      rows="10"
      [(ngModel)]="textValue"
    ></textarea>
  </div>
  <button (click)="addContainer()">Add More</button>
</div>

.ts File

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-sample',
  templateUrl: './sample.component.html',
  styleUrls: ['./sample.component.css'],
})
export class SampleComponent implements OnInit {
  constructor() {}
  container: any[] = [0];
  keyValue: String = '';
  textValue: String = '';
  newArray: any = [];
  ngOnInit(): void {}

  addContainer() {
    this.newArray.push({ key: this.keyValue, values: this.textValue });
    console.log(this.newArray);

    this.container.push(this.container.length);
    console.log(this.container);
  }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You should be using reactive form to build forms, if for a very simple form you want to create it like this.

HTML:

<div *ngFor="let ele of container; let i = index">

  <div >
    <input type="text" [(ngModel)]="newArray[i].values" />
    <textarea
      name=""
      id=""
      cols="30"
      rows="10"
      [(ngModel)]="newArray[i].textValue"
    ></textarea>
  </div>
</div>
<button (click)="addContainer()">Add More</button>

TS:

container: any[] = [1];
  newArray: any = [{ key: 1, values: '' }];
  ngOnInit(): void {}

  addContainer() {
    let keyValue = this.container.length + 1;
    this.newArray.push({ key: keyValue, values: '' });
    console.log(this.newArray);

    this.container.push(keyValue);
    console.log(this.container);
  }
over 4 years ago · Santiago Trujillo 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