Dynamic FormArray for checkboxes

I am creating a form in angular2 and typescript. I am trying to add a list of checkboxes dynamically and it does not work for me, where is my error?

Template Code:

<div formArrayName="categories"> <div class="form-group" *ngFor="let category of updateDetailsForm.controls.categories.controls; let i = index"> <label> <input type="checkbox" formControlName="{โ€Œ{i}}"> {โ€Œ{category.name}} </label> </div> </div> 

Typescript Code:

 updateDetailsForm: FormGroup; private categories : Category[] = [] constructor(private router: Router, private ss : SearchService, private formBuilder: FormBuilder) { this.initForm() // before the categories loaded this.ss.getAllCategories().subscribe( data => { this.categories = data this.initForm() // refresh the form with categories } ) } initForm() { let allCategories: FormArray = new FormArray([]); for (let i = 0; i < this.categories.length; i++) { allCategories.push( new FormGroup({ 'name': new FormControl([this.categories[i].categoryName]) }) ) } this.updateDetailsForm = this.formBuilder.group({ 'image' : [''], 'categories': allCategories }) } 

This is my user interface result, and I get the following error:

"inline template: 17:33 called: control.registerOnChange is not a function"

enter image description here

The number of checkboxes is correct, but the text is missing, and I canโ€™t update the form result with the userโ€™s choice.

  • What does the error mean?

  • How can I insert the correct text after this checkbox?

  • How to update user selection in form value?

+5
source share
2 answers

You have created your form model in such a way that we need to access each FormGroup that you insert into the array, and then access the named control inside:

 <span formGroupName="{{i}}"> <input type="checkbox" formControlName="{{category.name}}"> {{category.name}} </span> 

We also need to configure how we push the values โ€‹โ€‹so that the name is given uniquely, and not always as "name" :

 let fg = new FormGroup({}); fg.addControl(this.categories[i].name, new FormControl(false)) allCategories.push(fg) 

Here's a plunker to demonstrate it: http://plnkr.co/edit/OTiqwr1oCb8uEThQyDHx?p=preview

+4
source

I think that due to the same categories name of your component variable and form group control, you get an error. Check this out by making a change to your component below and generating the HTML:

You can check the FormArrayName directive for more help.

//Component

 updateDetailsForm: FormGroup; private allCategories : Category[] = [] constructor(private router: Router, private ss : SearchService, private formBuilder: FormBuilder) { this.initForm() // before the categories loaded this.ss.getAllCategories().subscribe( data => { this.allCategories = data this.initForm() // refresh the form with categories } ) } initForm() { let allCategories: FormArray = new FormArray([]); for (let i = 0; i < this.allCategories.length; i++) { allCategories.push(new FormControl()); } this.updateDetailsForm = this.formBuilder.group({ 'image' : [''], 'categories': allCategories }) } 

// HTML form

 <div formArrayName="categories"> <div class="form-group" *ngFor="let category of categories.controls; let i = index"> <label> <input type="checkbox" formControlName="i"> {โ€Œ{allCategories[i].name}} </label> </div> </div> 
+4
source

Source: https://habr.com/ru/post/1260816/


All Articles