Problems adding array to a formGroup

0

I have the following form group:

variacaoForm: FormGroup;

This form group has a control called variacoes .

I need to add an array in this control in a certain index passed as a parameter in my function, the problem is that the variable I use to push (this.listAtributes) is accumulating and when I change the index it adds to the another index the array that should only be added in that index.

  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});
  }

That's my job. I have the following scenario:

I have a variation and it can have x attributes; The variable this.listAtributes will increment the attributes and then add in that index passed in the parameter.

The problem is that when you change the index, the array listAtributes should be zeroed. But I can not think of a way to do this.

@Edit:

I tried something like:

  adicionaAtributo(index: number){ //Adiciona atributo no form group
    this.verificaIndexs.push(index);

    if(!this.verificaIndexs.includes(index)){
      this.listAtributos= []
    }else{
      for(let i=0;i<this.listAtributos.length;i++){
        if(index == this.listAtributos[i].indexvariacaoatributo){
          this.verificaIndexs.push(this.listAtributos[i])
        }
      }

    }

    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});
  }

but I still can not get the logic right

    
asked by anonymous 13.11.2018 / 12:33

0 answers