Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

362
Visualizações
Fetch input values created by ngFor on button click Angular

I have an input field which is being generated using *ngFor and I want to capture all those values inside the input field on submit button click. I have tried all possible ways but not able to get the entered data, please help me. Below is my code I have tried.

html Code:

arr= [{name:"test", percentage: "29"},{name:"abc", percentage: "45"}, {name:"def", percentage: "63"}]


<div *ngFor= "let obj of arr">
<input type="text" [value]="obj.percentage">
</div>
<button (click)="submit()"></button>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your current solution does not have any sort of form manager. There are two solutions:

Reactive Forms

// component.ts
form = new FormGroup( {
  test: new FormControl(null)
  // Add your other fields here
} );

// Patch your values into the form
arr.forEach(value => {
  this.form.get(value.name).patchValue(value.percentage);
})

// html
<div *ngFor= "let obj of arr">
  <input type="text" [formControl]="form.get(obj.name)" >
</div>

Your form will store all the values. To get them once, you could use form.value

NgModel

// component.ts
test: boolean;
// Add your other fields here

// Patch your values into the form
arr.forEach(value => {
  this.[value.name] = value.percentage;
})

// html
<div *ngFor= "let obj of arr">
  <input type="text" [(ngModel)]="obj.name" >
</div>

Both of these examples are cutting corners but they should give you enough of an idea of where to head next. I'd suggest the Reactive Forms path.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda