I want to output a form in which one field is an array, and for each element of the array I need to output my inputs.
Component:
userData.contacts = [
{contact_type: "phone", value: "380666666666"},
{contact_type: "email", value: "[email protected]"},
{contact_type: "website", value: "www.good.co"}
];
this.contactInfoForm = new FormGroup ({
contacts: this.fb.array (userData.contacts)
});
HTML:
<form [formGroup] = "contactInfoForm" novalidate (ngSubmit) = "submit ('contactInfoForm')">
<div formArrayName = "contacts" * ngFor = "let contact of contactInfoForm.get ('contacts'). controls; let i = index;">
<div formGroupName = "i">
<label> {{contact.value.contact_type}} </ label>
<input formControlName = "value">
</ div>
</ div>
</ form>
If to deduce in the console the form that there an object with a heap of the data.
asyncValidator: null
controls:
contacts: FormArray
asyncValidator: null
controls: Array (3)
0: FormControl {validator: null, asyncValidator: null, _onCollectionChange: ƒ, pristine: true, touched: false, ...}
1: FormControl {validator: null, asyncValidator: null, _onCollectionChange: ƒ, pristine: true, touched: false, ...}
2: FormControl {validator: null, asyncValidator: null, _onCollectionChange: ƒ, pristine: true, touched: false, ...}
length: 3
__proto__: Array (0)
dirty: (...)
disabled: (...)
enabled: (...)
errors: null
invalid: (...)
length: (...)
parent: (...)
pending: (...)
pristine: true
root: (...)
status: DISABLED
statusChanges: EventEmitter {_isScalar: false, observers: Array (0), closed: false, isStopped: false, hasError: false, ...}
touched: false
untouched: (...)
updateOn: (...)
valid: (...)
validator: null
value: (3) [{...}, {...}, {...}]
valueChanges: EventEmitter {_isScalar: false, observers: Array (0), closed: false, isStopped: false, hasError: false, ...}
_onCollectionChange: ƒ ()
_onDisabledChange: []
_parent: FormGroup {validator: null, asyncValidator: null, _onCollectionChange: ƒ, pristine: true, touched: false, ...}
__proto__: AbstractControl
__proto__: Object
dirty: (...)
disabled: (...)
enabled: (...)
errors: null
invalid: (...)
parent: (...)
pending: (...)
pristine: true
root: (...)
status: DISABLED
statusChanges: EventEmitter {_isScalar: false, observers: Array (0), closed: false, isStopped: false, hasError: false, ...}
touched: false
untouched: (...)
updateOn: (...)
valid: (...)
validator: null
value: {contacts: Array (3)}
valueChanges: EventEmitter {_isScalar: false, observers: Array (0), closed: false, isStopped: false, hasError: false, ...}
_onCollectionChange: ƒ ()
_onDisabledChange: []
__proto__: AbstractControl
I have tried to derive the information in different ways. It does not work. Even if stupidly copy-paste pieces from examples in the Internet. The same problems. The same mistakes.
Error:
Cannot find control with path: 'contacts -> 0 -> value'
Also tried:
<input [formControlName]="contact.value.value">
Got error:
contacts -> 0 -> 380666666666
*ngForshould not be within yourformArrayName=...element but one child below. You shouldngForchildren offormArray.