如何以角度6

时间:2018-11-20 06:06:05

标签: angular

我有一个表单,它具有固定字段以及从数据库创建的动态字段。字段每次都可能不同。因此,如何访问此类类别对象的JSON格式的动态生成的formControl值。

输出为:-

Category:[
  {
    "label": "test1",
    "measurements": {
        length:10,
        Shoulder:20,
        Chest: 20
    }
  },
  {
    "label": "test2",
    "measurements": {
        stomach:10,
        thigh:20 }
  }
]

enter image description here

<div *ngFor="let item of clickCategoryData let pointIndex=index">
<p><b>{{item.label}}</b></p>
<div *ngFor="let measurement of item.measurements;">
<label>{{measurement.name}}</label>
<input type="number" class="box" [placeholder]="measurement.name" [id]="measurement.id"  required>
</div>
</div>

1 个答案:

答案 0 :(得分:3)

将现有数组转换为此所需的输出数组

this.clickCategoryData.forEach(
      data => {
        let jsonObj = {}
        data.measurements.forEach(mes => jsonObj[mes.name] = "")
          data.measurements = jsonObj
      }
    )

这是stackBlitz Link