Skip to content

Reactive forms - can't add additional data with FormArray #13845

@ArielGueta

Description

@ArielGueta

I'm submitting a ...

[ x ] bug report => search github for a similar issue or PR before submitting
[ x ] feature request

Current behavior
When working with FormArray, there is no way to add additional data to the markup.

Expected behavior
We need a way to add additional data to the markup.

What is the motivation / use case for changing the behavior?
The best example is labels for checkboxes and radio buttons.

@Component({
  selector: 'my-app',
  template: `
    
     <form [formGroup]="checkboxGroup">
      <div formArrayName="food">
        <div *ngFor="let control of foodArray.controls; let i=index">
          <input [formControlName]="i" type="checkbox">
          // I don't have a way to set label for example
        </div>
      </div>
     </form>
     
    <div>{{checkboxGroup.value | json}}</div>
     { "food": [ true, false, true ] }
  `,
})
export class App {

  checkboxGroup: FormGroup;
  foodArray: FormArray;
  constructor(_fb: FormBuilder) {

    this.foodArray = new FormArray(
      [
        new FormControl(true),
        new FormControl(false),
        new FormControl(true)
      ]
    );
    
    this.checkboxGroup = _fb.group({
      food: this.foodArray
    });
    
  }
}

There are two problems with this approach:

  1. I can't set a dynamic label for every input.
  2. It's useless in most of the case that the value of the food is an array of true/false. ( usually we are sending objects to the server )

The right way for my opinion is something like this:

new FormControl( { checked: true/false, label: "pizza", id: 3, ...more data } )

// The form food value
[{checked: true/false, label: "pizza", id: 3, ...more data}]
  • Angular version: 2.0.X

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions