0

我在从表单中获取 ID 时遇到问题。

this.myForm 看起来像这样 @Input() public myForm: FormGroup;

它是来自另一个 .ts 文件的输入

这是表单的结构:
myForm
 - firstname
 - lastname
 - Email
 --Tags (FormArray)
  ---id (FormGroup)

我想访问 id 并用一个用户的所有标签填充 this.selectedTagList ,这就是我想要做的:

const control: FormArray = <FormArray>this.myForm.controls[ 'tags' ];
control.controls.forEach(tag=> {
this.selectedTagList.push(this.tagList.find(tag.value.id));
});

如果我 console.log(this.myForm) 我得到这个结构:
这个.myForm

console.log(control.controls), 'controls' 来自上面的代码:
在此处输入图像描述

但是当我执行 console.log(control.controls.length) 时,我得到 0。或者当我执行 console.log(control.controls[0]) 时,我得到未定义。

我不知道 FormGroup 对象去了哪里,也不知道为什么它说 Array[0] 里面有 4 个 FormGroup 对象。

4

2 回答 2

0

尝试像约翰所说的那样,你所拥有的不是数字索引数组,而是你有一个具有命名索引的对象(如关联数组)。

在 JavaScript 中,数组使用编号索引。在 JavaScript 中,对象使用命名索引。命名索引与关联数组相同。

欲了解更多信息,请参阅:https ://www.w3schools.com/js/js_arrays.asp

于 2017-03-27T07:31:14.050 回答
0

试试 console.log(this.myForm.controls['id']);

于 2017-03-24T15:54:44.307 回答