Angular Reactive Forms with FormArray inside a nested FormGroup





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've found a lot of examples, also other questions that have been answered. Including this one that seems pretty clear, but I still can't make my case work.



I have the following Angular reactive form (which seems ok, if I compare to the linked question's answer):



this.frm = this.fb.group({
// ... a bunch of other groups
// and then:

custom_data: this.fb.group({
pairs: this.fb.array([
this.fb.group({
custom_key: '',
custom_value: ''
})
]),
expire_date: '',
active: ['', Validators.required]
})
});


I can't make it render in the view, no matter what I've tried, I get an error. This is the latest/current piece of code that I got to, after all the other answers and google results I could find:



<form [formGroup]="frm">

<fieldset>
<legend>Custom Data:</legend>

<ng-container formGroupName="custom_data">
<div class="row">

<div formArrayName="pairs">
<div *ngFor="let pair of custom_data.get('pairs').controls; let i = index">
<div [formGroupName]="i">
<input type="text" formControlName="custom_key" placeholder="Custom key" />
<input type="text" formControlName="custom_value" placeholder="Custom value" />
</div>
</div>
</div> <!-- / end FormArray -->

<div class="col-xs-6">
<div class="form-group">
<label class="control-label">Expires</label>
<input type="text" formControlName="expire_date" class="form-control" />
</div>
</div>

<div class="col-xs-6">
<div class="form-group">
<label class="control-label">Active<sup>*</sup></label>
<select formControlName="active" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>

</div>
</ng-container>
</fieldset>
</form>


I've tried all of the following, without any luck:



<div *ngFor="let pair of custom_data.get('pairs').controls; let i = index">



<div *ngFor="let pair of pairs.controls; let i = index">



<div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">



I'm not even sure if a FormArray is a Control or a Group or what is it, every usage I find seems so unique and complicated.










share|improve this question























  • You must push an element to array "pairs", so you can see it in the view.

    – Yoarthur
    Dec 13 '18 at 0:29











  • Can you please provide a codepen / fiddle link of you code?

    – Rupesh
    Dec 13 '18 at 0:33











  • Try < form *ngIf="frm" [formGroup]="frm" >. This "*ngIf" avoid an error when your frm is not defined (at very early time in your aplicattion -before you give data-)

    – Eliseo
    Dec 13 '18 at 7:47


















0















I've found a lot of examples, also other questions that have been answered. Including this one that seems pretty clear, but I still can't make my case work.



I have the following Angular reactive form (which seems ok, if I compare to the linked question's answer):



this.frm = this.fb.group({
// ... a bunch of other groups
// and then:

custom_data: this.fb.group({
pairs: this.fb.array([
this.fb.group({
custom_key: '',
custom_value: ''
})
]),
expire_date: '',
active: ['', Validators.required]
})
});


I can't make it render in the view, no matter what I've tried, I get an error. This is the latest/current piece of code that I got to, after all the other answers and google results I could find:



<form [formGroup]="frm">

<fieldset>
<legend>Custom Data:</legend>

<ng-container formGroupName="custom_data">
<div class="row">

<div formArrayName="pairs">
<div *ngFor="let pair of custom_data.get('pairs').controls; let i = index">
<div [formGroupName]="i">
<input type="text" formControlName="custom_key" placeholder="Custom key" />
<input type="text" formControlName="custom_value" placeholder="Custom value" />
</div>
</div>
</div> <!-- / end FormArray -->

<div class="col-xs-6">
<div class="form-group">
<label class="control-label">Expires</label>
<input type="text" formControlName="expire_date" class="form-control" />
</div>
</div>

<div class="col-xs-6">
<div class="form-group">
<label class="control-label">Active<sup>*</sup></label>
<select formControlName="active" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>

</div>
</ng-container>
</fieldset>
</form>


I've tried all of the following, without any luck:



<div *ngFor="let pair of custom_data.get('pairs').controls; let i = index">



<div *ngFor="let pair of pairs.controls; let i = index">



<div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">



I'm not even sure if a FormArray is a Control or a Group or what is it, every usage I find seems so unique and complicated.










share|improve this question























  • You must push an element to array "pairs", so you can see it in the view.

    – Yoarthur
    Dec 13 '18 at 0:29











  • Can you please provide a codepen / fiddle link of you code?

    – Rupesh
    Dec 13 '18 at 0:33











  • Try < form *ngIf="frm" [formGroup]="frm" >. This "*ngIf" avoid an error when your frm is not defined (at very early time in your aplicattion -before you give data-)

    – Eliseo
    Dec 13 '18 at 7:47














0












0








0








I've found a lot of examples, also other questions that have been answered. Including this one that seems pretty clear, but I still can't make my case work.



I have the following Angular reactive form (which seems ok, if I compare to the linked question's answer):



this.frm = this.fb.group({
// ... a bunch of other groups
// and then:

custom_data: this.fb.group({
pairs: this.fb.array([
this.fb.group({
custom_key: '',
custom_value: ''
})
]),
expire_date: '',
active: ['', Validators.required]
})
});


I can't make it render in the view, no matter what I've tried, I get an error. This is the latest/current piece of code that I got to, after all the other answers and google results I could find:



<form [formGroup]="frm">

<fieldset>
<legend>Custom Data:</legend>

<ng-container formGroupName="custom_data">
<div class="row">

<div formArrayName="pairs">
<div *ngFor="let pair of custom_data.get('pairs').controls; let i = index">
<div [formGroupName]="i">
<input type="text" formControlName="custom_key" placeholder="Custom key" />
<input type="text" formControlName="custom_value" placeholder="Custom value" />
</div>
</div>
</div> <!-- / end FormArray -->

<div class="col-xs-6">
<div class="form-group">
<label class="control-label">Expires</label>
<input type="text" formControlName="expire_date" class="form-control" />
</div>
</div>

<div class="col-xs-6">
<div class="form-group">
<label class="control-label">Active<sup>*</sup></label>
<select formControlName="active" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>

</div>
</ng-container>
</fieldset>
</form>


I've tried all of the following, without any luck:



<div *ngFor="let pair of custom_data.get('pairs').controls; let i = index">



<div *ngFor="let pair of pairs.controls; let i = index">



<div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">



I'm not even sure if a FormArray is a Control or a Group or what is it, every usage I find seems so unique and complicated.










share|improve this question














I've found a lot of examples, also other questions that have been answered. Including this one that seems pretty clear, but I still can't make my case work.



I have the following Angular reactive form (which seems ok, if I compare to the linked question's answer):



this.frm = this.fb.group({
// ... a bunch of other groups
// and then:

custom_data: this.fb.group({
pairs: this.fb.array([
this.fb.group({
custom_key: '',
custom_value: ''
})
]),
expire_date: '',
active: ['', Validators.required]
})
});


I can't make it render in the view, no matter what I've tried, I get an error. This is the latest/current piece of code that I got to, after all the other answers and google results I could find:



<form [formGroup]="frm">

<fieldset>
<legend>Custom Data:</legend>

<ng-container formGroupName="custom_data">
<div class="row">

<div formArrayName="pairs">
<div *ngFor="let pair of custom_data.get('pairs').controls; let i = index">
<div [formGroupName]="i">
<input type="text" formControlName="custom_key" placeholder="Custom key" />
<input type="text" formControlName="custom_value" placeholder="Custom value" />
</div>
</div>
</div> <!-- / end FormArray -->

<div class="col-xs-6">
<div class="form-group">
<label class="control-label">Expires</label>
<input type="text" formControlName="expire_date" class="form-control" />
</div>
</div>

<div class="col-xs-6">
<div class="form-group">
<label class="control-label">Active<sup>*</sup></label>
<select formControlName="active" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>

</div>
</ng-container>
</fieldset>
</form>


I've tried all of the following, without any luck:



<div *ngFor="let pair of custom_data.get('pairs').controls; let i = index">



<div *ngFor="let pair of pairs.controls; let i = index">



<div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">



I'm not even sure if a FormArray is a Control or a Group or what is it, every usage I find seems so unique and complicated.







angular angular-reactive-forms formarray






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 13 '18 at 0:09









MrCroftMrCroft

1,73412239




1,73412239













  • You must push an element to array "pairs", so you can see it in the view.

    – Yoarthur
    Dec 13 '18 at 0:29











  • Can you please provide a codepen / fiddle link of you code?

    – Rupesh
    Dec 13 '18 at 0:33











  • Try < form *ngIf="frm" [formGroup]="frm" >. This "*ngIf" avoid an error when your frm is not defined (at very early time in your aplicattion -before you give data-)

    – Eliseo
    Dec 13 '18 at 7:47



















  • You must push an element to array "pairs", so you can see it in the view.

    – Yoarthur
    Dec 13 '18 at 0:29











  • Can you please provide a codepen / fiddle link of you code?

    – Rupesh
    Dec 13 '18 at 0:33











  • Try < form *ngIf="frm" [formGroup]="frm" >. This "*ngIf" avoid an error when your frm is not defined (at very early time in your aplicattion -before you give data-)

    – Eliseo
    Dec 13 '18 at 7:47

















You must push an element to array "pairs", so you can see it in the view.

– Yoarthur
Dec 13 '18 at 0:29





You must push an element to array "pairs", so you can see it in the view.

– Yoarthur
Dec 13 '18 at 0:29













Can you please provide a codepen / fiddle link of you code?

– Rupesh
Dec 13 '18 at 0:33





Can you please provide a codepen / fiddle link of you code?

– Rupesh
Dec 13 '18 at 0:33













Try < form *ngIf="frm" [formGroup]="frm" >. This "*ngIf" avoid an error when your frm is not defined (at very early time in your aplicattion -before you give data-)

– Eliseo
Dec 13 '18 at 7:47





Try < form *ngIf="frm" [formGroup]="frm" >. This "*ngIf" avoid an error when your frm is not defined (at very early time in your aplicattion -before you give data-)

– Eliseo
Dec 13 '18 at 7:47












2 Answers
2






active

oldest

votes


















0














You must push to the array, you can check this link for example






share|improve this answer































    0














    So I ended up redoing it from scratch.
    Weird thing... it turns out that the 3rd thing I've tried eventually worked:



    <div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">


    Not sure why it didn't work the first time.



    One thing to note: I also discovered that I get an error when doing a production build. In order to solve that, I had to add this method in my component's class:



    myFormArray() {
    return this.frm.controls['custom_data'].get('pairs') as FormArray;
    }


    and then, in my template, use the method:



    <div *ngFor="let pair of myFormArray().controls; let i = index">


    (which is really weird and shouldn't be necessary, if you ask me...)






    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',
      autoActivateHeartbeat: false,
      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%2f53753257%2fangular-reactive-forms-with-formarray-inside-a-nested-formgroup%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You must push to the array, you can check this link for example






      share|improve this answer




























        0














        You must push to the array, you can check this link for example






        share|improve this answer


























          0












          0








          0







          You must push to the array, you can check this link for example






          share|improve this answer













          You must push to the array, you can check this link for example







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 13 '18 at 0:47









          Muhammad Al FarisMuhammad Al Faris

          363




          363

























              0














              So I ended up redoing it from scratch.
              Weird thing... it turns out that the 3rd thing I've tried eventually worked:



              <div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">


              Not sure why it didn't work the first time.



              One thing to note: I also discovered that I get an error when doing a production build. In order to solve that, I had to add this method in my component's class:



              myFormArray() {
              return this.frm.controls['custom_data'].get('pairs') as FormArray;
              }


              and then, in my template, use the method:



              <div *ngFor="let pair of myFormArray().controls; let i = index">


              (which is really weird and shouldn't be necessary, if you ask me...)






              share|improve this answer




























                0














                So I ended up redoing it from scratch.
                Weird thing... it turns out that the 3rd thing I've tried eventually worked:



                <div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">


                Not sure why it didn't work the first time.



                One thing to note: I also discovered that I get an error when doing a production build. In order to solve that, I had to add this method in my component's class:



                myFormArray() {
                return this.frm.controls['custom_data'].get('pairs') as FormArray;
                }


                and then, in my template, use the method:



                <div *ngFor="let pair of myFormArray().controls; let i = index">


                (which is really weird and shouldn't be necessary, if you ask me...)






                share|improve this answer


























                  0












                  0








                  0







                  So I ended up redoing it from scratch.
                  Weird thing... it turns out that the 3rd thing I've tried eventually worked:



                  <div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">


                  Not sure why it didn't work the first time.



                  One thing to note: I also discovered that I get an error when doing a production build. In order to solve that, I had to add this method in my component's class:



                  myFormArray() {
                  return this.frm.controls['custom_data'].get('pairs') as FormArray;
                  }


                  and then, in my template, use the method:



                  <div *ngFor="let pair of myFormArray().controls; let i = index">


                  (which is really weird and shouldn't be necessary, if you ask me...)






                  share|improve this answer













                  So I ended up redoing it from scratch.
                  Weird thing... it turns out that the 3rd thing I've tried eventually worked:



                  <div *ngFor="let pair of frm.controls['custom_data'].get('pairs').controls; let i = index">


                  Not sure why it didn't work the first time.



                  One thing to note: I also discovered that I get an error when doing a production build. In order to solve that, I had to add this method in my component's class:



                  myFormArray() {
                  return this.frm.controls['custom_data'].get('pairs') as FormArray;
                  }


                  and then, in my template, use the method:



                  <div *ngFor="let pair of myFormArray().controls; let i = index">


                  (which is really weird and shouldn't be necessary, if you ask me...)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 4 at 18:40









                  MrCroftMrCroft

                  1,73412239




                  1,73412239






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53753257%2fangular-reactive-forms-with-formarray-inside-a-nested-formgroup%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas