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;
}
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
add a comment |
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
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
add a comment |
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
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
angular angular-reactive-forms formarray
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
You must push to the array, you can check this link for example
add a comment |
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...)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You must push to the array, you can check this link for example
add a comment |
You must push to the array, you can check this link for example
add a comment |
You must push to the array, you can check this link for example
You must push to the array, you can check this link for example
answered Dec 13 '18 at 0:47
Muhammad Al FarisMuhammad Al Faris
363
363
add a comment |
add a comment |
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...)
add a comment |
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...)
add a comment |
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...)
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...)
answered Jan 4 at 18:40
MrCroftMrCroft
1,73412239
1,73412239
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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