Is there a way to use a FormArray as the top level form

Multi tool use
up vote
1
down vote
favorite
I'm using @ngular/core 6.1.1 and @angular/forms 6.1.1
I'm trying to write a ControlValueAccessor which will manage an array of values.
I'm using reactive forms and declaring my form like this:
export class ItemsFormComponent implements ControlValueAccor, OnInit {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(this.items.map((item) => new FormControl(item)));
this.form.valueChanges
.debounceTime(200)
.subscribe((items: string) => {
this.onChangeFn(items);
});
}
}
But then, I get a Template parsing error with
<ol [formArray]="form">
...
</ol>
Can't bind to 'formArray' since it isn't a known property of 'ol'
I DID include the FormsModule and the ReactiveFormsModule in my Feature Module. Any other form based on FormGroup work.
It kinda works if I do <ol [formGroup]="form">
but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway.
So is there no way to use a FormArray as the top level form? Ideally, I'd like to avoid creating a FormGroup with a single FormArray in it.
angular angular-forms
add a comment |
up vote
1
down vote
favorite
I'm using @ngular/core 6.1.1 and @angular/forms 6.1.1
I'm trying to write a ControlValueAccessor which will manage an array of values.
I'm using reactive forms and declaring my form like this:
export class ItemsFormComponent implements ControlValueAccor, OnInit {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(this.items.map((item) => new FormControl(item)));
this.form.valueChanges
.debounceTime(200)
.subscribe((items: string) => {
this.onChangeFn(items);
});
}
}
But then, I get a Template parsing error with
<ol [formArray]="form">
...
</ol>
Can't bind to 'formArray' since it isn't a known property of 'ol'
I DID include the FormsModule and the ReactiveFormsModule in my Feature Module. Any other form based on FormGroup work.
It kinda works if I do <ol [formGroup]="form">
but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway.
So is there no way to use a FormArray as the top level form? Ideally, I'd like to avoid creating a FormGroup with a single FormArray in it.
angular angular-forms
"It kinda works if I do <ol [formGroup]="form"> but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway." Why? For me is correct
– Eliseo
Nov 10 at 13:36
OK, it builds in prod with<ol [formGroup]="form">
but it still looks like a hack to me..
– paztek
Nov 10 at 13:49
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm using @ngular/core 6.1.1 and @angular/forms 6.1.1
I'm trying to write a ControlValueAccessor which will manage an array of values.
I'm using reactive forms and declaring my form like this:
export class ItemsFormComponent implements ControlValueAccor, OnInit {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(this.items.map((item) => new FormControl(item)));
this.form.valueChanges
.debounceTime(200)
.subscribe((items: string) => {
this.onChangeFn(items);
});
}
}
But then, I get a Template parsing error with
<ol [formArray]="form">
...
</ol>
Can't bind to 'formArray' since it isn't a known property of 'ol'
I DID include the FormsModule and the ReactiveFormsModule in my Feature Module. Any other form based on FormGroup work.
It kinda works if I do <ol [formGroup]="form">
but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway.
So is there no way to use a FormArray as the top level form? Ideally, I'd like to avoid creating a FormGroup with a single FormArray in it.
angular angular-forms
I'm using @ngular/core 6.1.1 and @angular/forms 6.1.1
I'm trying to write a ControlValueAccessor which will manage an array of values.
I'm using reactive forms and declaring my form like this:
export class ItemsFormComponent implements ControlValueAccor, OnInit {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(this.items.map((item) => new FormControl(item)));
this.form.valueChanges
.debounceTime(200)
.subscribe((items: string) => {
this.onChangeFn(items);
});
}
}
But then, I get a Template parsing error with
<ol [formArray]="form">
...
</ol>
Can't bind to 'formArray' since it isn't a known property of 'ol'
I DID include the FormsModule and the ReactiveFormsModule in my Feature Module. Any other form based on FormGroup work.
It kinda works if I do <ol [formGroup]="form">
but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway.
So is there no way to use a FormArray as the top level form? Ideally, I'd like to avoid creating a FormGroup with a single FormArray in it.
angular angular-forms
angular angular-forms
asked Nov 10 at 12:59
paztek
9116
9116
"It kinda works if I do <ol [formGroup]="form"> but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway." Why? For me is correct
– Eliseo
Nov 10 at 13:36
OK, it builds in prod with<ol [formGroup]="form">
but it still looks like a hack to me..
– paztek
Nov 10 at 13:49
add a comment |
"It kinda works if I do <ol [formGroup]="form"> but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway." Why? For me is correct
– Eliseo
Nov 10 at 13:36
OK, it builds in prod with<ol [formGroup]="form">
but it still looks like a hack to me..
– paztek
Nov 10 at 13:49
"It kinda works if I do <ol [formGroup]="form"> but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway." Why? For me is correct
– Eliseo
Nov 10 at 13:36
"It kinda works if I do <ol [formGroup]="form"> but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway." Why? For me is correct
– Eliseo
Nov 10 at 13:36
OK, it builds in prod with
<ol [formGroup]="form">
but it still looks like a hack to me..– paztek
Nov 10 at 13:49
OK, it builds in prod with
<ol [formGroup]="form">
but it still looks like a hack to me..– paztek
Nov 10 at 13:49
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Yes you can use FormArray as the top level form,
place: FormArray = new FormArray([
new FormControl('SF'),
new FormControl('NY'),
]);
And in the template,
<div *ngFor="let c of place.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of place.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
Here is the working demo on stackblitz
add a comment |
up vote
0
down vote
What you have done so far is pretty close except wrong form object reference.
Your modified code
html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div *ngFor="let c of form.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of form.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
ts
export class AppComponent {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(
this.items.map((item) => new FormControl(item))
);
this.form.valueChanges.subscribe(changes=>{
console.log("changes to form ", changes);
});
}
}
Working demo is here - https://stackblitz.com/edit/angular-em7j9h
Note : you should ignore
FormArray
in html otherwise you need will be forced to haveFormGroup
.
How is my answer different than yours?
– anonymous
Nov 10 at 19:14
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Yes you can use FormArray as the top level form,
place: FormArray = new FormArray([
new FormControl('SF'),
new FormControl('NY'),
]);
And in the template,
<div *ngFor="let c of place.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of place.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
Here is the working demo on stackblitz
add a comment |
up vote
2
down vote
accepted
Yes you can use FormArray as the top level form,
place: FormArray = new FormArray([
new FormControl('SF'),
new FormControl('NY'),
]);
And in the template,
<div *ngFor="let c of place.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of place.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
Here is the working demo on stackblitz
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Yes you can use FormArray as the top level form,
place: FormArray = new FormArray([
new FormControl('SF'),
new FormControl('NY'),
]);
And in the template,
<div *ngFor="let c of place.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of place.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
Here is the working demo on stackblitz
Yes you can use FormArray as the top level form,
place: FormArray = new FormArray([
new FormControl('SF'),
new FormControl('NY'),
]);
And in the template,
<div *ngFor="let c of place.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of place.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
Here is the working demo on stackblitz
answered Nov 10 at 14:00


anonymous
185115
185115
add a comment |
add a comment |
up vote
0
down vote
What you have done so far is pretty close except wrong form object reference.
Your modified code
html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div *ngFor="let c of form.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of form.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
ts
export class AppComponent {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(
this.items.map((item) => new FormControl(item))
);
this.form.valueChanges.subscribe(changes=>{
console.log("changes to form ", changes);
});
}
}
Working demo is here - https://stackblitz.com/edit/angular-em7j9h
Note : you should ignore
FormArray
in html otherwise you need will be forced to haveFormGroup
.
How is my answer different than yours?
– anonymous
Nov 10 at 19:14
add a comment |
up vote
0
down vote
What you have done so far is pretty close except wrong form object reference.
Your modified code
html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div *ngFor="let c of form.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of form.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
ts
export class AppComponent {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(
this.items.map((item) => new FormControl(item))
);
this.form.valueChanges.subscribe(changes=>{
console.log("changes to form ", changes);
});
}
}
Working demo is here - https://stackblitz.com/edit/angular-em7j9h
Note : you should ignore
FormArray
in html otherwise you need will be forced to haveFormGroup
.
How is my answer different than yours?
– anonymous
Nov 10 at 19:14
add a comment |
up vote
0
down vote
up vote
0
down vote
What you have done so far is pretty close except wrong form object reference.
Your modified code
html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div *ngFor="let c of form.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of form.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
ts
export class AppComponent {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(
this.items.map((item) => new FormControl(item))
);
this.form.valueChanges.subscribe(changes=>{
console.log("changes to form ", changes);
});
}
}
Working demo is here - https://stackblitz.com/edit/angular-em7j9h
Note : you should ignore
FormArray
in html otherwise you need will be forced to haveFormGroup
.
What you have done so far is pretty close except wrong form object reference.
Your modified code
html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div *ngFor="let c of form.controls; index as i">
<input [formControl]="c" placeholder="c">
</div>
<ng-container *ngFor="let c of form.controls">
<pre>{{this.c.value | json}}</pre>
</ng-container>
ts
export class AppComponent {
public form: FormArray;
public items: string = ['foo', 'bar', 'baz'];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.array(
this.items.map((item) => new FormControl(item))
);
this.form.valueChanges.subscribe(changes=>{
console.log("changes to form ", changes);
});
}
}
Working demo is here - https://stackblitz.com/edit/angular-em7j9h
Note : you should ignore
FormArray
in html otherwise you need will be forced to haveFormGroup
.
answered Nov 10 at 17:08


Sunil Singh
4,6811624
4,6811624
How is my answer different than yours?
– anonymous
Nov 10 at 19:14
add a comment |
How is my answer different than yours?
– anonymous
Nov 10 at 19:14
How is my answer different than yours?
– anonymous
Nov 10 at 19:14
How is my answer different than yours?
– anonymous
Nov 10 at 19:14
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239189%2fis-there-a-way-to-use-a-formarray-as-the-top-level-form%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
k0tdjAB gdsRLCT,nrdhbS,glH6E1 ASdFBZ wawPVJhoPx3uKPL,we rh5LUE,uoPHDEkNuUpYgWqHoIM0erDBdtYd,6sT,3 3b
"It kinda works if I do <ol [formGroup]="form"> but it seems like a weird hack and the validation seems broken. And I'm afraid it will break when building for prod anyway." Why? For me is correct
– Eliseo
Nov 10 at 13:36
OK, it builds in prod with
<ol [formGroup]="form">
but it still looks like a hack to me..– paztek
Nov 10 at 13:49