如何保存 *ngFor 生成的输入数据

How to save the data of the inputs generated by *ngFor

所以,我正在使用 Angular 5,我有两个数组,其中包含从数据库中提取的数据:

["Reports", "UI"]

0: {ranges: "Low", ph: 0, days: 0}
1: {ranges: "Mediu,", ph: 0, days: 0}
2: {ranges: "High", ph: 0, days: 0}

我为第一个数组中的每个元素显示了第二个数组。之后我有一个输入并像我对第二个数组所做的那样重复它

    ...
    <tbody *ngFor="let i of paramsData; let y = index"> <---Repeat the data from first array
      <ng-container *ngFor="let data of times; let x = index"> <---Repeat the data from second array in each element in the first array
        <tr>
          <td [attr.rowspan]="times.length" *ngIf="x == 0">{{i}}</td>
          <td>{{data.ranges}}</td>
          <td>
            <input type="number" class="form-control" name='weight'>
          </td>
          <td>{{data.ph}}</td>
          <td>{{data.days}}</td>
        </tr>
      </ng-container>
    </tbody>
    ...

问题:如何获取由 *ngFor 自动生成的输入中引入的值?我无法在 formControlName 中赋值,因为所有输入都具有相同的名称,并且只会保存一个值。

此外,我不能使用 [(ngModel)]="someArray[x]" 将值存储在数组中,因为它只会在第二次 ngFor 上保存第一次迭代的值。

我是不是丢了什么东西?有没有办法做到这一点?

-- 编辑--

Link 到 StackBlitz:https://stackblitz.com/edit/angular-ux4cqv


table

的截图

Demo

您可以将表单实现为二维数组:

app.component.ts

import { Component } from '@angular/core';
import { FormGroup, FormArray, FormControl } from '@angular/forms';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  formGroup: FormGroup;
  params = ["Reports", "UI"];
  ranges = [
    {ranges: "Low", ph: 0, days: 0},
    {ranges: "Medium,", ph: 0, days: 0},
    {ranges: "High", ph: 0, days: 0}
  ]
  constructor() {
    var paramsArray = new FormArray([]);
    this.params.forEach(t=> {
      var rangeArray = new FormArray([]);
      paramsArray.push(rangeArray);
      this.ranges.forEach(t=> {
        rangeArray.push(new FormControl(''))
      });
    });

    this.formGroup = new FormGroup({
      "values": paramsArray
    });
  }
}

app.component.html

<div [formGroup]="formGroup">
<table border="1" formArrayName="values">
    <thead>
        <tr>
            <th>Param. de Estimación</th>
            <th>Complejidad</th>
            <th>Peso</th>
            <th>Product. Hora</th>
            <th>Esf. en Dias</th>
        </tr>
    </thead>
    <tbody *ngFor="let i of params; index as y" [formArrayName]="y">
        <ng-container *ngFor="let data of ranges; index as x">
            <tr>
                <td [attr.rowspan]="ranges.length" *ngIf="x == 0">{{i}}</td>
                <td>{{data.ranges}}</td>
                <td>
                    <input type="number" class="form-control" [formControlName]="x" placeholder="Weight" name='weight'>
                </td>
                <td>{{data.ph}}</td>
                <td>{{data.days}}</td>
            </tr>
        </ng-container>
        <button type="submit" *ngIf="y==1">Pto. de Referencia</button>
    </tbody>
</table>
</div>

表格值被保存到一个"values"二维数组,你可以通过调用formGroup.value:

得到
{
  "values": [
    [
      "",
      "",
      ""
    ],
    [
      "",
      "",
      ""
    ]
  ]
}