Skip to main content
added 110 characters in body
Source Link
C0mpl3x
  • 532
  • 8
  • 28

Let's say I have a json with these values:

{
car: bmw,
tunes : [{
 name: turbo,
 cost: 350
},
{
 name: exhaust,
 cost: 100
}]
}

How do I create a form that will be able to take these values? FormGroup won't work I think. FormArray seems like the right thing to use but I haven't seen it being used with pre selected fields that can be assigned values to, which is what I need. I need to be able to assign array of object to the from, edit certain values of the objects and export the form as a json with the array of objects (same as the array came but maybe with different values). The fields wont change so I don't need to add them dynamically, I just need to assign them.

My imagined solution would look something like this:

form: New FormGroup({
 car: new FormControl(),
 tunes: new FormArray([
  name: new FormControl(),
  cost: new FormControl()
 ])
})

Update:

This seems to be the wanted solution:

form: New FormGroup({
     car: new FormControl(),
     tunes: new FormArray([
      new FormGroup({
       name: new FormControl(),
       cost: new FormControl()
      })
     ])
    })

but it gives me error: "Cannot find form control at index 1"

When I look at the json it produces, it is the wanted outcome but I'm not able to assign values right now.

Let's say I have a json with these values:

{
car: bmw,
tunes : [{
 name: turbo,
 cost: 350
},
{
 name: exhaust,
 cost: 100
}]
}

How do I create a form that will be able to take these values? FormGroup won't work I think. FormArray seems like the right thing to use but I haven't seen it being used with pre selected fields that can be assigned values to, which is what I need. I need to be able to assign array of object to the from, edit certain values of the objects and export the form as a json with the array of objects (same as the array came but maybe with different values). The fields wont change so I don't need to add them dynamically, I just need to assign them.

My imagined solution would look something like this:

form: New FormGroup({
 car: new FormControl(),
 tunes: new FormArray([
  name: new FormControl(),
  cost: new FormControl()
 ])
})

Update:

This seems to be the wanted solution:

form: New FormGroup({
     car: new FormControl(),
     tunes: new FormArray([
      new FormGroup({
       name: new FormControl(),
       cost: new FormControl()
      })
     ])
    })

but it gives me error: "Cannot find form control at index 1"

Let's say I have a json with these values:

{
car: bmw,
tunes : [{
 name: turbo,
 cost: 350
},
{
 name: exhaust,
 cost: 100
}]
}

How do I create a form that will be able to take these values? FormGroup won't work I think. FormArray seems like the right thing to use but I haven't seen it being used with pre selected fields that can be assigned values to, which is what I need. I need to be able to assign array of object to the from, edit certain values of the objects and export the form as a json with the array of objects (same as the array came but maybe with different values). The fields wont change so I don't need to add them dynamically, I just need to assign them.

My imagined solution would look something like this:

form: New FormGroup({
 car: new FormControl(),
 tunes: new FormArray([
  name: new FormControl(),
  cost: new FormControl()
 ])
})

Update:

This seems to be the wanted solution:

form: New FormGroup({
     car: new FormControl(),
     tunes: new FormArray([
      new FormGroup({
       name: new FormControl(),
       cost: new FormControl()
      })
     ])
    })

but it gives me error: "Cannot find form control at index 1"

When I look at the json it produces, it is the wanted outcome but I'm not able to assign values right now.

Added more explanation
Source Link
C0mpl3x
  • 532
  • 8
  • 28

Let's say I have a json with these values:

{
car: bmw,
tunes : [{
 name: turbo,
 cost: 350
},
{
 name: exhaust,
 cost: 100
}]
}

How do I create a form that will be able to take these values? FormGroup won't work I think. FormArray seems like the right thing to use but I haven't seen it being used with pre selected fields that can be assigned values to, which is what I need. So myI need to be able to assign array of object to the from, edit certain values of the objects and export the form as a json with the array of objects (same as the array came but maybe with different values). The fields wont change so I don't need to add them dynamically, I just need to assign them.

My imagined solution would look something like this:

form: New FormGroup({
 car: new FormControl(),
 tunes: new FormArray([
  name: new FormControl(),
  cost: new FormControl()
 ])
})

Update:

This seems to be the wanted solution:

form: New FormGroup({
     car: new FormControl(),
     tunes: new FormArray([
      new FormGroup({
       name: new FormControl(),
       cost: new FormControl()
      })
     ])
    })

but it gives me error: "Cannot find form control at index 1"

Let's say I have a json with these values:

{
car: bmw,
tunes : [{
 name: turbo,
 cost: 350
},
{
 name: exhaust,
 cost: 100
}]
}

How do I create a form that will be able to take these values? FormGroup won't work I think. FormArray seems like the right thing to use but I haven't seen it being used with pre selected fields that can be assigned values to, which is what I need. So my imagined solution would look something like this:

form: New FormGroup({
 car: new FormControl(),
 tunes: new FormArray([
  name: new FormControl(),
  cost: new FormControl()
 ])
})

Update:

This seems to be the wanted solution:

form: New FormGroup({
     car: new FormControl(),
     tunes: new FormArray([
      new FormGroup({
       name: new FormControl(),
       cost: new FormControl()
      })
     ])
    })

but it gives me error: "Cannot find form control at index 1"

Let's say I have a json with these values:

{
car: bmw,
tunes : [{
 name: turbo,
 cost: 350
},
{
 name: exhaust,
 cost: 100
}]
}

How do I create a form that will be able to take these values? FormGroup won't work I think. FormArray seems like the right thing to use but I haven't seen it being used with pre selected fields that can be assigned values to, which is what I need. I need to be able to assign array of object to the from, edit certain values of the objects and export the form as a json with the array of objects (same as the array came but maybe with different values). The fields wont change so I don't need to add them dynamically, I just need to assign them.

My imagined solution would look something like this:

form: New FormGroup({
 car: new FormControl(),
 tunes: new FormArray([
  name: new FormControl(),
  cost: new FormControl()
 ])
})

Update:

This seems to be the wanted solution:

form: New FormGroup({
     car: new FormControl(),
     tunes: new FormArray([
      new FormGroup({
       name: new FormControl(),
       cost: new FormControl()
      })
     ])
    })

but it gives me error: "Cannot find form control at index 1"

changed the title
Link
C0mpl3x
  • 532
  • 8
  • 28

How to add json array dataof objects to angular form

deleted 2 characters in body
Source Link
C0mpl3x
  • 532
  • 8
  • 28
Loading
added 294 characters in body
Source Link
C0mpl3x
  • 532
  • 8
  • 28
Loading
added 294 characters in body
Source Link
C0mpl3x
  • 532
  • 8
  • 28
Loading
Source Link
C0mpl3x
  • 532
  • 8
  • 28
Loading