Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

javascript - Populate withJSON data formArray Angular 11

I want to edit a data but I don't know how can I popule the form again with the json response from my API

In my response i get my json

project: {
    "id": 1,
    "customer": "Jhonathan",
    "steps": "{"id":1,"paso":"Paso 1","accion":"accion 1","punto_critico":"punto critico 1","razon":"razon 1","fichero":"fileImage","namefile":"3.jpg"}",
    "epp": "{"zapatos":"zapatos","guantes":"guantes","mandil":"mandil","lentes":"lentes","faja":"faja","chaleco":"chaleco","tapones":"tapones","mascarilla":"mascarilla","otro":""}",
    "turns": "["2 Turno"]",
    "created_at": "2021-01-13T15:20:49.000000Z",
    "updated_at": "2021-01-15T02:20:33.000000Z",
    "deleted_at": null
}

I've my form in this way

editProject = this.formBuilder.group({
  id: [1],
  customer: [''],
  steps: this.formBuilder.array([this.newStep]),
  epp: new FormGroup({
    zapatos: new FormControl(''),
    guantes: new FormControl(''),
    mandil: new FormControl(''),
    lentes: new FormControl(''),
    faja: new FormControl(''),
    chaleco: new FormControl(''),
    tapones: new FormControl(''),
    mascarilla: new FormControl(''),
    otro: new FormControl(''),
  }),
  turns: [[]],
})

the form that i create the step array is with this

    getNewId() {
    this.currentStepId += 1;
    return this.currentStepId;
  }

  get newStep(): FormGroup {
    return this.formBuilder.group({
      id: [this.getNewId()],
      paso: ['Paso ' + this.currentStepId],
      accion: [''],
      punto_critico: [''],
      razon: [''],
      fichero: ['']
    })
  }

  get steps(): FormArray {
    return this.editProject.get('steps') as FormArray
  }

  Addsteps() {
    this.steps.push(this.newStep);
  }

Is my firts time trying to do this. I tray making like this but I think that i got a infinit loop because my app never stop charging and it crash

for(let line = 0; line < response.project.steps.length; line++){
   const linesFormArray = this.editProject.get('steps') as FormArray;
   linesFormArray.push(this.newStep);
}

hope someone can help me

question from:https://stackoverflow.com/questions/65829732/populate-withjson-data-formarray-angular-11

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...