How can I define an array inside my formgroup and push data?











up vote
0
down vote

favorite












I have this function that create my formgroup in my ngOnInit:



ngOnInit() {

//When start component create my form group
this.variacaoForm = this.fb.group({

variacoes: this.fb.array([this.createFormGroup()])

});


createFormGroup(): FormGroup {
return this.fb.group({
atributo: "",
preco: null,
listaatributos: ,
sku: '',
tipo: '',
id: '',
id_produto: '',
estoque_variacao: 0,
linkfotovariacao: '',
created_at: '',
foto_prin_1: '',
foto_prin_2: '',
foto_prin_3: '',
foto_prin_4: '',
foto_prin_5: '',
foto_prin_6: ''
});
}
}


Note that listaatributos must be an array.



I try add one function that push data in this listaatributos array:



adicionaAtributo(index: number) {          
this.variacaoForm.value.variacoes[index].listaatributos.push(this.idAtributo);
}


But i have this message:




ERROR TypeError: Cannot read property 'listaatributos' of undefined




My idea is that i have a reactive form that must be various variacoes and inside the variacoes must be a listaatributos set as array.



Something like:



variações: [{"estoque_variacao": 900, "atributos":[12,13]}]


This is the tree dom of my variaçãoForm:



Controls dom
Value dom










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have this function that create my formgroup in my ngOnInit:



    ngOnInit() {

    //When start component create my form group
    this.variacaoForm = this.fb.group({

    variacoes: this.fb.array([this.createFormGroup()])

    });


    createFormGroup(): FormGroup {
    return this.fb.group({
    atributo: "",
    preco: null,
    listaatributos: ,
    sku: '',
    tipo: '',
    id: '',
    id_produto: '',
    estoque_variacao: 0,
    linkfotovariacao: '',
    created_at: '',
    foto_prin_1: '',
    foto_prin_2: '',
    foto_prin_3: '',
    foto_prin_4: '',
    foto_prin_5: '',
    foto_prin_6: ''
    });
    }
    }


    Note that listaatributos must be an array.



    I try add one function that push data in this listaatributos array:



    adicionaAtributo(index: number) {          
    this.variacaoForm.value.variacoes[index].listaatributos.push(this.idAtributo);
    }


    But i have this message:




    ERROR TypeError: Cannot read property 'listaatributos' of undefined




    My idea is that i have a reactive form that must be various variacoes and inside the variacoes must be a listaatributos set as array.



    Something like:



    variações: [{"estoque_variacao": 900, "atributos":[12,13]}]


    This is the tree dom of my variaçãoForm:



    Controls dom
    Value dom










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have this function that create my formgroup in my ngOnInit:



      ngOnInit() {

      //When start component create my form group
      this.variacaoForm = this.fb.group({

      variacoes: this.fb.array([this.createFormGroup()])

      });


      createFormGroup(): FormGroup {
      return this.fb.group({
      atributo: "",
      preco: null,
      listaatributos: ,
      sku: '',
      tipo: '',
      id: '',
      id_produto: '',
      estoque_variacao: 0,
      linkfotovariacao: '',
      created_at: '',
      foto_prin_1: '',
      foto_prin_2: '',
      foto_prin_3: '',
      foto_prin_4: '',
      foto_prin_5: '',
      foto_prin_6: ''
      });
      }
      }


      Note that listaatributos must be an array.



      I try add one function that push data in this listaatributos array:



      adicionaAtributo(index: number) {          
      this.variacaoForm.value.variacoes[index].listaatributos.push(this.idAtributo);
      }


      But i have this message:




      ERROR TypeError: Cannot read property 'listaatributos' of undefined




      My idea is that i have a reactive form that must be various variacoes and inside the variacoes must be a listaatributos set as array.



      Something like:



      variações: [{"estoque_variacao": 900, "atributos":[12,13]}]


      This is the tree dom of my variaçãoForm:



      Controls dom
      Value dom










      share|improve this question















      I have this function that create my formgroup in my ngOnInit:



      ngOnInit() {

      //When start component create my form group
      this.variacaoForm = this.fb.group({

      variacoes: this.fb.array([this.createFormGroup()])

      });


      createFormGroup(): FormGroup {
      return this.fb.group({
      atributo: "",
      preco: null,
      listaatributos: ,
      sku: '',
      tipo: '',
      id: '',
      id_produto: '',
      estoque_variacao: 0,
      linkfotovariacao: '',
      created_at: '',
      foto_prin_1: '',
      foto_prin_2: '',
      foto_prin_3: '',
      foto_prin_4: '',
      foto_prin_5: '',
      foto_prin_6: ''
      });
      }
      }


      Note that listaatributos must be an array.



      I try add one function that push data in this listaatributos array:



      adicionaAtributo(index: number) {          
      this.variacaoForm.value.variacoes[index].listaatributos.push(this.idAtributo);
      }


      But i have this message:




      ERROR TypeError: Cannot read property 'listaatributos' of undefined




      My idea is that i have a reactive form that must be various variacoes and inside the variacoes must be a listaatributos set as array.



      Something like:



      variações: [{"estoque_variacao": 900, "atributos":[12,13]}]


      This is the tree dom of my variaçãoForm:



      Controls dom
      Value dom







      angular typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 11:19









      Dyd666

      324316




      324316










      asked Nov 8 at 10:42









      Renato Veronese

      317




      317
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Use this.variacaoForm.controls instead of this.variacaoForm.value :



          adicionaAtributo(index: number) {          
          (<FormGroup>(<FormArray>this.variacaoForm.controls['variacoes']).controls[index]).controls['listaatributos'].push(this.idAtributo);
          }


          Regards,






          share|improve this answer























          • After ['variacoes'].>controls< i have [ts] Property 'controls' does not exist on type 'AbstractControl'.
            – Renato Veronese
            Nov 8 at 11:28










          • i updated the answer , can you please check it now ( a cast to FormArray should be added )
            – Mohamed Ali RACHID
            Nov 8 at 11:32










          • Already tried: this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo) but now i have Cannot read property 'listaatributos' of undefined
            – Renato Veronese
            Nov 8 at 11:33










          • Tried with your suggestion: (<FormArray>this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo)) but now i must have: Cannot read property 'listaatributos' of undefined
            – Renato Veronese
            Nov 8 at 11:35










          • Obs: if i try exactly when you did: " (<FormArray>this.variacaoForm.controls['variacoes']).controls[index].listaatributos.push(this.idAtributo);" i receive: Property 'listaatributos' does not exist on type 'AbstractControl'.
            – Renato Veronese
            Nov 8 at 11:36


















          up vote
          0
          down vote













          What worked for me:



           adicionaAtributo(index: number){ //Adiciona atributo no form group
          this.listAtributos.push({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});





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206056%2fhow-can-i-define-an-array-inside-my-formgroup-and-push-data%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Use this.variacaoForm.controls instead of this.variacaoForm.value :



            adicionaAtributo(index: number) {          
            (<FormGroup>(<FormArray>this.variacaoForm.controls['variacoes']).controls[index]).controls['listaatributos'].push(this.idAtributo);
            }


            Regards,






            share|improve this answer























            • After ['variacoes'].>controls< i have [ts] Property 'controls' does not exist on type 'AbstractControl'.
              – Renato Veronese
              Nov 8 at 11:28










            • i updated the answer , can you please check it now ( a cast to FormArray should be added )
              – Mohamed Ali RACHID
              Nov 8 at 11:32










            • Already tried: this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo) but now i have Cannot read property 'listaatributos' of undefined
              – Renato Veronese
              Nov 8 at 11:33










            • Tried with your suggestion: (<FormArray>this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo)) but now i must have: Cannot read property 'listaatributos' of undefined
              – Renato Veronese
              Nov 8 at 11:35










            • Obs: if i try exactly when you did: " (<FormArray>this.variacaoForm.controls['variacoes']).controls[index].listaatributos.push(this.idAtributo);" i receive: Property 'listaatributos' does not exist on type 'AbstractControl'.
              – Renato Veronese
              Nov 8 at 11:36















            up vote
            0
            down vote













            Use this.variacaoForm.controls instead of this.variacaoForm.value :



            adicionaAtributo(index: number) {          
            (<FormGroup>(<FormArray>this.variacaoForm.controls['variacoes']).controls[index]).controls['listaatributos'].push(this.idAtributo);
            }


            Regards,






            share|improve this answer























            • After ['variacoes'].>controls< i have [ts] Property 'controls' does not exist on type 'AbstractControl'.
              – Renato Veronese
              Nov 8 at 11:28










            • i updated the answer , can you please check it now ( a cast to FormArray should be added )
              – Mohamed Ali RACHID
              Nov 8 at 11:32










            • Already tried: this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo) but now i have Cannot read property 'listaatributos' of undefined
              – Renato Veronese
              Nov 8 at 11:33










            • Tried with your suggestion: (<FormArray>this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo)) but now i must have: Cannot read property 'listaatributos' of undefined
              – Renato Veronese
              Nov 8 at 11:35










            • Obs: if i try exactly when you did: " (<FormArray>this.variacaoForm.controls['variacoes']).controls[index].listaatributos.push(this.idAtributo);" i receive: Property 'listaatributos' does not exist on type 'AbstractControl'.
              – Renato Veronese
              Nov 8 at 11:36













            up vote
            0
            down vote










            up vote
            0
            down vote









            Use this.variacaoForm.controls instead of this.variacaoForm.value :



            adicionaAtributo(index: number) {          
            (<FormGroup>(<FormArray>this.variacaoForm.controls['variacoes']).controls[index]).controls['listaatributos'].push(this.idAtributo);
            }


            Regards,






            share|improve this answer














            Use this.variacaoForm.controls instead of this.variacaoForm.value :



            adicionaAtributo(index: number) {          
            (<FormGroup>(<FormArray>this.variacaoForm.controls['variacoes']).controls[index]).controls['listaatributos'].push(this.idAtributo);
            }


            Regards,







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 11:38

























            answered Nov 8 at 11:24









            Mohamed Ali RACHID

            1,736313




            1,736313












            • After ['variacoes'].>controls< i have [ts] Property 'controls' does not exist on type 'AbstractControl'.
              – Renato Veronese
              Nov 8 at 11:28










            • i updated the answer , can you please check it now ( a cast to FormArray should be added )
              – Mohamed Ali RACHID
              Nov 8 at 11:32










            • Already tried: this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo) but now i have Cannot read property 'listaatributos' of undefined
              – Renato Veronese
              Nov 8 at 11:33










            • Tried with your suggestion: (<FormArray>this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo)) but now i must have: Cannot read property 'listaatributos' of undefined
              – Renato Veronese
              Nov 8 at 11:35










            • Obs: if i try exactly when you did: " (<FormArray>this.variacaoForm.controls['variacoes']).controls[index].listaatributos.push(this.idAtributo);" i receive: Property 'listaatributos' does not exist on type 'AbstractControl'.
              – Renato Veronese
              Nov 8 at 11:36


















            • After ['variacoes'].>controls< i have [ts] Property 'controls' does not exist on type 'AbstractControl'.
              – Renato Veronese
              Nov 8 at 11:28










            • i updated the answer , can you please check it now ( a cast to FormArray should be added )
              – Mohamed Ali RACHID
              Nov 8 at 11:32










            • Already tried: this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo) but now i have Cannot read property 'listaatributos' of undefined
              – Renato Veronese
              Nov 8 at 11:33










            • Tried with your suggestion: (<FormArray>this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo)) but now i must have: Cannot read property 'listaatributos' of undefined
              – Renato Veronese
              Nov 8 at 11:35










            • Obs: if i try exactly when you did: " (<FormArray>this.variacaoForm.controls['variacoes']).controls[index].listaatributos.push(this.idAtributo);" i receive: Property 'listaatributos' does not exist on type 'AbstractControl'.
              – Renato Veronese
              Nov 8 at 11:36
















            After ['variacoes'].>controls< i have [ts] Property 'controls' does not exist on type 'AbstractControl'.
            – Renato Veronese
            Nov 8 at 11:28




            After ['variacoes'].>controls< i have [ts] Property 'controls' does not exist on type 'AbstractControl'.
            – Renato Veronese
            Nov 8 at 11:28












            i updated the answer , can you please check it now ( a cast to FormArray should be added )
            – Mohamed Ali RACHID
            Nov 8 at 11:32




            i updated the answer , can you please check it now ( a cast to FormArray should be added )
            – Mohamed Ali RACHID
            Nov 8 at 11:32












            Already tried: this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo) but now i have Cannot read property 'listaatributos' of undefined
            – Renato Veronese
            Nov 8 at 11:33




            Already tried: this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo) but now i have Cannot read property 'listaatributos' of undefined
            – Renato Veronese
            Nov 8 at 11:33












            Tried with your suggestion: (<FormArray>this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo)) but now i must have: Cannot read property 'listaatributos' of undefined
            – Renato Veronese
            Nov 8 at 11:35




            Tried with your suggestion: (<FormArray>this.variacaoForm.controls['variacoes']['controls'][index].listaatributos.push(this.idAtributo)) but now i must have: Cannot read property 'listaatributos' of undefined
            – Renato Veronese
            Nov 8 at 11:35












            Obs: if i try exactly when you did: " (<FormArray>this.variacaoForm.controls['variacoes']).controls[index].listaatributos.push(this.idAtributo);" i receive: Property 'listaatributos' does not exist on type 'AbstractControl'.
            – Renato Veronese
            Nov 8 at 11:36




            Obs: if i try exactly when you did: " (<FormArray>this.variacaoForm.controls['variacoes']).controls[index].listaatributos.push(this.idAtributo);" i receive: Property 'listaatributos' does not exist on type 'AbstractControl'.
            – Renato Veronese
            Nov 8 at 11:36












            up vote
            0
            down vote













            What worked for me:



             adicionaAtributo(index: number){ //Adiciona atributo no form group
            this.listAtributos.push({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});





            share|improve this answer

























              up vote
              0
              down vote













              What worked for me:



               adicionaAtributo(index: number){ //Adiciona atributo no form group
              this.listAtributos.push({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});





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                What worked for me:



                 adicionaAtributo(index: number){ //Adiciona atributo no form group
                this.listAtributos.push({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});





                share|improve this answer












                What worked for me:



                 adicionaAtributo(index: number){ //Adiciona atributo no form group
                this.listAtributos.push({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});






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 12:30









                Renaot PLS

                57




                57






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206056%2fhow-can-i-define-an-array-inside-my-formgroup-and-push-data%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff