Javascript 错误类型错误:value.forEach不是函数
我需要用FormArray从angular 6中创建一个更新 我在Javascript 错误类型错误:value.forEach不是函数,javascript,angular,typescript,angular6,Javascript,Angular,Typescript,Angular6,我需要用FormArray从angular 6中创建一个更新 我在editfrom.TS中使用此代码: valueIngrident=new FormArray([]); constructor(private brandService:BrandService,private PValueInfoService:ProductinfovalueService,private productinfoService:ProductinfoService,private catServ
editfrom.TS
中使用此代码:
valueIngrident=new FormArray([]);
constructor(private brandService:BrandService,private PValueInfoService:ProductinfovalueService,private productinfoService:ProductinfoService,private catService:CategoryService,private produSrvice:ProductService, private route:ActivatedRoute,private fb:FormBuilder) {
this.id= + this.route.snapshot.paramMap.get('id');
this.productVM=this.produSrvice.InitialProduct();
}
ngOnInit() {
this.InitialForm()
this.GetMainCat();
}
InitialForm(){
this.editFG=this.fb.group({
productTitle:['',Validators.compose([Validators.required])],
productName:['',Validators.compose([Validators.required])],
color:['',Validators.compose([Validators.required])],
productImageName:['',Validators.compose([Validators.required])],
price:['',Validators.compose([Validators.required])],
gurantyMonth:['',Validators.compose([Validators.required])],
gurantyCompanyName:['',Validators.compose([Validators.required])],
catId:['',Validators.compose([Validators.required])],
brandId:['',Validators.compose([Validators.required])],
values:this.valueIngrident
})
this.GetProductByID(this.id)
}
public GetProductByID(id:number){
this.produSrvice.GetProductDetailById(id).subscribe((data)=>{
this.productVM=data
this.SelectBrand=data.brandId
this.SelectCat=data.catId
this.PName=this.productVM.productName
this.editFG.setValue({
productTitle:[data.productTitle],
productName:[data.productName],
color:[data.color],
productImageName:[data.productImageName],
price:[data.price],
gurantyMonth:[data.gurantyMonth],
gurantyCompanyName:[data.gurantyCompanyName],
catId:[data.catId],
brandId:[data.brandId],
values:this.valueIngrident
})
this.ChangeFormByType(data.catId)
})
}
get ValueFormControl(){
return this.editFG.get('values') as FormArray;
}
public CreateValueFiled(PD:Productinfovalue[]){
for(let element of PD )
{
this.valueIngrident.push(
new FormGroup({
infoId:new FormControl(element.infoId),
value:new FormControl(element.value)
})
)
}
}
public ChangeFormByType(id:number){
this.ChangeBrand(id)
this.PValueInfoService.GetAllInfoValueForUpdate(id).subscribe(
res=>{
this.PD=res,
this.CreateValueFiled(this.PD)
}
)
}
我在HTML中使用它:
<div class="form-inline lbin" formArrayName="values">
<div class="form-inline lbin" *ngFor="let valueCtrl of ValueFormControl.controls; let i=index" [formGroupName]="i">
<div class="form-inline lbin">
<label></label>
<input formControlName="value" >
</div>
</div>
</div>
但当页面加载时,它会显示以下错误:
错误类型错误:value.forEach不是函数
在FormArray.push../node_modules/@angular/forms/fesm5/forms.js.FormArray.setValue(forms.js:3545)
有什么问题吗?FORMARY具有函数setValue。第一个参数应该是数组。您正在发送一个对象 查看函数的中间部分
GetProductByID(id:number)
在这里调用this.editFG.setValue({…})
:
setValue(值:任意[],选项:{onlySelf?:布尔值;emitEvent?:
boolean;}={}):void参数值的任何[]值数组
控制
选项对象配置用于确定控件运行方式的选项
传播更改并在值更改后发出事件
onlySelf:如果为true,则每个更改只影响此控件,而不影响
它的父母。默认值为false。emitEvent:当为true或未提供时
(默认设置),可以观察到状态更改和值更改
当控件值为时,发出具有最新状态和值的事件
更新。如果为false,则不会发出任何事件。配置选项
传递给updateValueAndValidity方法。可选。默认为
{}
返回无效
您的参数被包装在{}中,这使它成为一个对象。函数规范说它需要是一个数组
setValue函数在数组上调用forEach,而对象没有forEach,这就是为什么它说value.forEach不是函数。需要检查this.editFG.get('values')的返回,因为它不是数组。您应该防御性地测试该返回,因为它来自不受控制的来源。如果无效,则抛出并处理或返回一个空数组。什么是
this.valueIngrident
?@User5842valueIngrident=new-FormArray([])
@RandyCasburn值
是表单Array@Mr-程序员-阅读下面的答案!