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

321
Visualizações
How can I take the value of an <input> in an *ngfor?

I have a table of variable length and in every cells I have an input(the default value of every input is 0), I need the user to edit the data that he wants, then when he press a button I need to have all the data back and the id of the input (the id is always different), even the ones that still haven't been edited, I'm going to attach the code for better understanding:

<form  #login (ngSubmit)="onSubmit(login.value)" novalidate>
    <table class="rtable">
        <tr>
            <th></th>
            <th *ngFor="let column of mesi">
                {{column}}
            </th>
        </tr>

        <tr *ngFor="let row of FigProfs">
            {{row.nome}}
            <td *ngFor="let column of mesi">
                <input  id="gd{{row.id}}_{{column}}" type="number"  value=0 >
            </td>
            
        </tr>
    </table>
    <button class="btnsubmit" type="submit">AVANTI</button>
</form>

So as you can see the input is always different because is generated using values that changes depending on the values of the ngfor, so my question is: Is there an easy way to have in my ts file an array of all this values?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The straightforward solution, bind ngModel to an object:

<input  id="gd{{row.id}}_{{column}}" type="number"  value=0 [ngModel]="get(row.id,column)" (ngModelChange)="set(row.id,column,$event)">

Your class:

data: any = {};

get(i, j) {
  return this.data[i] && this.data[i][j];
}

set(i, j, v) {
  this.data[i] = this.data[i] || {};
  this.data[i][j] = v;
}

Feel free to set the types properly.

Stackblitz demo

over 4 years ago · Santiago Trujillo Relatório

0

Use ng-Model with direct binding to nested array elements.

Template

    <tr *ngFor="let row of FigProfs;  let i = index">
        {{row.nome}}
        <td *ngFor="let column of mesi; let j = index">
            <input  id="gd{{row.id}}_{{column}}" type="number"  [(ngModel)]="inputValues(i, j)" >
        </td>
        
    </tr>

TS

let inputValues = new Array(this.FigProfs.length).fill(new Array(this.mesi.length).fill(0));
over 4 years ago · Santiago Trujillo 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