Error on create form array
core.js:6260 ERROR TypeError: control.registerOnChange is not a function at setUpModelChangePipeline (forms.js:3528) at setUpControl (forms.js:3405) at FormControlDirective.ngOnChanges (forms.js:7382) at FormControlDirective.wrapOnChangesHook_inPreviousChangesStorage (core.js:26975) at callHook (core.js:4762) at callHooks (core.js:4722) at executeInitAndCheckHooks (core.js:4662) at selectIndexInternal (core.js:9724) at Module.ɵɵadvance (core.js:9685) at BuildingPartsModalComponent_div_6_Template (test.component.html:37)
Here is my code Html
<form [formGroup]="demoForm">
<div formArrayName="demoArray" *ngFor="let arrayItem of arrayItems; let i=index">
<input [id]="arrayItem.parts" type="checkbox" [formControl]="arrayItems[i]">
<label [for]="arrayItem.volume" class="array-item-title">
{{arrayItem.name}}</label>
<div (click)="removeItem(i)">remove item</div>
</div>
<button (click)="submit()">Go</button>
</form>
<div (click)="addItem()">Add new</div>
TS
demoForm: FormGroup;
arrayItems: {
parts: number;
name: string;
volume: string;
}[];
ngOnInit(): void {
this.arrayItems = [{
parts: 11,
name: 'Naslov',
volume: '120'
}];
this.demoForm = this.formBuilder.group({
demoArray: new FormArray([])
});
}
get demoArray() {
return this.demoForm.get('demoArray') as FormArray;
}
addItem() {
const item = {
parts: 11,
name: 'Naslov2',
volume: '120'
};
this.arrayItems.push(item);
this.demoArray.push(this.formBuilder.control(false));
}
removeItem(index) {
this.arrayItems.splice(index, 1);
this.demoArray.removeAt(this.demoArray.length - 1);
}
submit() {
console.log(this.demoForm.value);
}
I made it much simpler that everybody can understand and to help me find the problem, thanks
.htmldoesn't work? what makes you thinks so?[formGroupName]="i"?, the array of is of typeFormControlrather thanFormGroup–