【发布时间】:2019-02-14 14:55:46
【问题描述】:
我想输出一个表单,其中一个字段是一个数组,对于数组的每个元素,我需要输出我的输入。
组件:
userData.contacts = [
{contact_type: "phone", value: "380666666666"},
{contact_type: "email", value: "ggg@gg.gg"},
{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>
如果在控制台中推断出有一个对象与一堆数据的形式。
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
我尝试以不同的方式获取信息。这是行不通的。即使愚蠢地从互联网上的示例中复制粘贴片段。同样的问题。同样的错误。
错误:
Cannot find control with path: 'contacts -> 0 -> value'
也试过了:
<input [formControlName]="contact.value.value">
出现错误:
联系人 -> 0 -> 380666666666
【问题讨论】:
-
您的
*ngFor不应在您的formArrayName=...元素内,而应在其下方一个子元素内。你应该ngForformArray的孩子。 -
我以前试过。也没有工作。但我现在再试一次
-
没有帮助。没有任何改变。
标签: angular forms angular-reactive-forms