I've built a custom input component with ControlValueAccessor and it works great to add tags as selections. (Stackblitz)
My problem is:
- when I implement the component within a form (cities & states controls)
- add values to both controls by selecting some options
- submit the form
- sometimes the control value is an array of selected tags (as expected)
- other times it's an actual FormArray object
Here is a screenshot of two of the same component's values after submitting the angular form. One is an array of objects (expected) the other is an actual FormArray object which .value property contains the array of objects!
Here is some code of how it works in case you don't want to visit StackBlitz.
The custom control is implemented like this.
this.form = this.fb.group({
tags: this.fb.array([])
});
When the user selects a dropdown item or hits enter the object is saved like this:
get tagsArray(): FormArray { return this.form.get('tags') as FormArray; }
...
this.tagsArray.push(new FormControl(value));
this.onChange(this.tagsArray); // update controller value
You could replicate this by implementing the component in my StackBlitz in a formGroup just like this (also in my StackBlitz):
Form Init
public form: FormGroup = this.fb.group({
states: [],
cities: []
});
Template
<input-tags formControlName="cities" label="Cities" [typeAhead]="cities" [displayKeys]="['name']" filterKeys="['name']"></input-tags>
<input-tags formControlName="states" label="States" [typeAhead]="states" [displayKeys]="['name']" filterKeys="['name']"></input-tags>
but the question is:
When is Angular's FormArray a traditional array and when is it a FormArray Array like object?