gpt4 book ai didi

angular - FormArray TypeError : value. forEach 不是函数

转载 作者:太空狗 更新时间:2023-10-29 17:32:09 24 4
gpt4 key购买 nike

为什么我总是收到这个错误?我认为我做的一切都正确吗?

“异常:未捕获( promise ):TypeError:value.forEach 不是函数TypeError: value.forEach 不是 FormArray.setValue 中的函数"

组件类:

jobDetails: FormGroup;
techFormArray: FormArray;

constructor(private formBuilder: FormBuilder){

this.techFormArray = new FormArray([
new FormControl(''),
new FormControl(''),
new FormControl('')
]);
this.jobDetails = this.formBuilder.group({
techs: this.formBuilder.array([])
});
this.jobDetails.setValue({
techs: this.techFormArray
});
}

HTML:

<form [formGroup]="jobDetails">
<div formArrayName="techs" >
<div style="display: flex; flex-direction: column">
<div *ngFor="let tech of techFormArray.controls; let i=index">
<md-checkbox [formControlName]="i">
{{i}}
</md-checkbox>
</div>
</div>
</div>
</form>

解决方案:

FunStuff 是正确的,我不能使用 setValue....所以我删除了它。问题解决了哈哈。我改变了一些东西,我不确定我到底做了什么,这基本上是几个小时的暴力试验和错误,但后来它起作用了! :)

这是工作版本!

jobDetails: FormGroup;

constructor(private formBuilder: FormBuilder){

this.jobDetails = formBuilder.group({
techs: formBuilder.array([
formBuilder.control(''),
formBuilder.control('')
])
});
}

新的 HTML:

    <form [formGroup]="jobDetails">
<div formArrayName="techs" >
<div style="display: flex; flex-direction: column">
<div *ngFor="let tech of jobDetails.controls.techs.controls; let i=index">
<md-checkbox [formControlName]="i">
{{i}}
</md-checkbox>
</div>
</div>
</div>
</form>

最佳答案

setValue 方法采用简单的值:

   this.techFormArray = new FormArray([
new FormControl(''),
new FormControl(''),
new FormControl('')
]);

this.jobDetails = this.formBuilder.group({
techs: this.techFormArray
});

this.jobDetails.setValue({
techs: ['a', 'b', 'c']
});

关于angular - FormArray TypeError : value. forEach 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41177069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com