如何从数组为 Angular 中的表单创建动态名称?

How to create dynamic names from an array for a form in Angular?

我需要发送动态表单,但我不知道如何为表单创建名称。

这是我的代码:

Ts

export class MiPage implements OnInit {

  public inputs = [];
  FormData: any;
  DataForm: FormGroup = new FormGroup({});
  payLoad: any;

 constructor() { }

generateFormControls(formData: any)
    {
        let tempGroup: FormGroup = new FormGroup({});
        formData.forEach(i=>{

            console.info(i.name);
            tempGroup.addControl(i.name, new FormControl(''))
        })
        return tempGroup;
    }

    PreviewData() 
    {
         this.payLoad = JSON.stringify(this.DataForm.value);
    }

    data() {
      
       for (let numero of resp.datos){

        this.inputs[numero.id_producto]=numero.id_producto;  //the array
             
      }
     
     //So I manually create the names, but this is dynamic because it is generated from a query in the database

  this.FormData = [
      {
      name:'429'
    },{
      name:'428'
    }]

//But I need to create it dynamically. For example, but it gives me syntax error un "For"

this.FormData = [
      
      for(let datos of this.inputs) {

        name: datos

      }  
    
    ]


  }
}

非常感谢你,希望你能帮助我。并且知道是否可以这样做或者我如何动态创建名称

您可能 push 将元素放入数组中。

this.FormData = [];
  
for(let datos of this.inputs) {

    this.FormData.push({name: datos});

}  

或者使用 map 更简洁的方式,正如@Eliseo 在下面的评论中所建议的那样:

this.FormData = this.inputs.map(x => ({name:x}));