ControlValueAccessor with FormArray
See original GitHub issueπ feature request
Relevant Package
This feature request is for @angular/formsDescription
I have a child component which deals with the `FormArray` of input controls. I want to have a formcontrol over the child component.I want to have a custom-child component say name-list. The parent FormControl will send FormArray to name-list. Then in NameListComponent will use FormArray and manipulate according to need just like FormControl do using ControlValueAccessor.
I have searched for similar functionality, I just come across this question without legitimate answer
Example
<h1>Child</h1>
<div formArrayName="names" *ngFor="let name of names.controls; let i = index;">
<div [formGroupName]="i" >
<input formControlName="firstName">
<input formControlName="lastName">
</div>
</div>
Intention is to bind parent form with the array of input control in the child component. Also child form will have validation checks.
Describe the solution youβd like
Need a similar `interface ControlValueAccessor` like `ArrayAccessorDescribe alternatives youβve considered
I have tried to send `formArrayName` to custom component, but it seems `writeValue(obj: any)` never called for `formArrayName`, it only works for `formControlName`.Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
ControlValueAccessor with FormArray in Angular 2
I want to have a formcontrol over the child component. I am passing the array of json object, what would be the correct...
Read more >Angular Nested Reactive Form Control Value Accessor
import { FormGroup, FormArray, FormControl,. Validators, FormBuilder, AbstractControl }. from '@angular/forms';. @Component({. selector: 'my-app',.
Read more >ControlValueAccessor with FormArray in Angular 2 - iTecNote
I have a child component which deals with the array of input controls. I want to have a formcontrol over the child component....
Read more >Don't reinvent the wheel when implementing ... - Medium
Reuse already-implemented controlValueAccessor ... controlContainer.control is the parent FormGroup (or FormArray) instance. */
Read more >Creating custom form controls using Control Value Accessor ...
How to create custom form controls in Angular using ControlValueAccessor? We can create custom form components and connect them to eitherΒ ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I donβt think this is a bug or missing feature; I believe youβre trying to misuse form controls.
If your child component has an array of controls, then itβs not a control. It would be an anti-pattern to have form controls inside something thatβs a form control itself: this is what form groups (
FormGroup) and form arrays (FormArray) have been created for. Your component is simply a component that should somehow be made aware that its controls are part of an externally created form. Not a control itself.I havenβt checked out
ngx-sub-formwhich @maxime1992 suggested above, but I guess itβs a nice wrapper around a technique Iβve described in this StackOverflow answer: https://stackoverflow.com/a/46025197/2131286.Basically, if you put a
formGroup,formGroupName,formArrayorformArrayNameon a component, then that component can pick up something called Control Container through injection. InjectControlContainerin the reusabile component, andControlContainer#controlwill give you the control, decided byformArrayNameor similar.This example, combined with the code from my previous comment, does what you want it to.
Iβll point out the obvious, you still have complete access to
topFormfrom the parent component.Iβm not sure what you mean by this. What do you think a hypothetical
ArrayValueAccessorwould do for you?