我有这个formGroup:
variacaoForm: FormGroup;
此表单组包含一个名为variacoes的控件。
我需要按传递给函数参数的确定索引在此控件中推送一个数组,问题是我用来推送(this.listAtributos)的数组积累了,当索引更改时必须重新初始化此数组。
我尝试:
adicionaAtributo(index: number){ //Adiciona atributo no form group
if(this.verificaIndexs.includes(index)){
for(let i=0;i<this.listAtributos.length;i++){
if(index == this.listAtributos[i].indexvariacaoatributo){
this.listAtributos.push(this.listAtributos[i])
}
}
}else{
this.listAtributos = [];
}
this.verificaIndexs.push(index);
this.listAtributos.push({indexvariacaoatributo: index, id:this.idAtributo, tipovariacao: this.variacaoForm.value.variacoes[index].tipo, valorvariacao: this.variacaoForm.value.variacoes[index].atributo})
const control = (<FormArray>this.variacaoForm.controls['variacoes']).at(index);
control.patchValue({listaatributos: this.listAtributos});
}
但不起作用,并导致内存泄漏**。 @Edit **
也请尝试在插入后排除:
adicionaAtributo(index: number){ //Adiciona atributo no form group
this.listAtributos.push({indexvariacaoatributo: index, id:this.idAtributo, tipovariacao: this.variacaoForm.value.variacoes[index].tipo, valorvariacao: this.variacaoForm.value.variacoes[index].atributo})
const control = (<FormArray>this.variacaoForm.controls['variacoes']).at(index);
control.patchValue({listaatributos: this.listAtributos});
for(let i=0;i<this.variacaoForm.value.variacoes.length;i++){
for(let j=0;j<this.variacaoForm.value.variacoes[i].listaatributos.length;j++){
if(this.variacaoForm.value.variacoes[i].listaatributos[j].indexvariacaoatributo !== index){
this.listAtributos.splice(j, 1)
}
}
}
control.patchValue({listaatributos: this.listAtributos});
}
但也不起作用:(