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
angular typescript
add a comment |
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
angular typescript
add a comment |
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
angular typescript
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
angular typescript
edited Nov 8 at 11:19
Dyd666
324316
324316
asked Nov 8 at 10:42
Renato Veronese
317
317
add a comment |
add a comment |
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,
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 toFormArray
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
|
show 1 more comment
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});
add a comment |
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,
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 toFormArray
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
|
show 1 more comment
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,
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 toFormArray
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
|
show 1 more comment
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,
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,
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 toFormArray
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
|
show 1 more comment
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 toFormArray
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
|
show 1 more comment
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});
add a comment |
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});
add a comment |
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});
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});
answered Nov 8 at 12:30
Renaot PLS
57
57
add a comment |
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%2f53206056%2fhow-can-i-define-an-array-inside-my-formgroup-and-push-data%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