1

Showing me error "TypeError: control.setParent is not a function" every time when try something like this....goal is to get answers('odgovori' FormArray) but not to loop on input but in radio group... than user select radio button as correct answer and store all. When do:

console.log(this.odgovorForm.value)

everything show ok but when push then show error

get odgovori() {
    return this.pitanjeForm.get('odgovori') as FormArray;
  }

 pitanjeForm = this.fb.group({
    data: ['', [Validators.required]],
    odgovori: this.fb.array([]),
    tacan: ['', [Validators.required]]
  });

  odgovorForm = this.fb.group({
    data: ['', [Validators.required]],
    color: 'danger'
  });

  addAnswer(): void {
    console.log(this.pitanjeForm.value, this.odgovorForm.value);
    this.info = this.odgovorForm.value;
    this.odgovori.push(this.info);
  }

HTML

<form [formGroup]="pitanjeForm">
 <div>
   <input formControlName="data">
 </div>
  <form [formGroup]="odgovorForm">
    <input formControlName="data">
    <button (click)="addAnswer()"></button>
  </form>
 <div *ngFor="let odgovor of odgovor.value; index as i">
   <input type="radio" value="{{ i }}"> 0 - 30<br>
 </div>
</form>

5 Answers 5

6

Oh..my mistake

addAnswer(): void {
 this.odgovori.push(this.odgovorForm)
}

TO PUSH FORMGROUP TO FORM ARRAY ALWAYS PUSH WHOLE FORMGROUP NOT JUST VALUE OR CONTROLS!

Sign up to request clarification or add additional context in comments.

2 Comments

It's the best answer
I have a problem when I want to push new object! All of old objects in array converted to the last one after push!
3

I put my code like Er Abdul Meman but I got Property push does not exist on type Error.

enter image description here

THIS WORKED FOR ME:

import { OnInit } from "@angular/core";
import { FormBuilder,Validators } from "@angular/forms";

export class YourClass implements OnInit {
  pitanjeForm = this.fb.group({
    articulo_alquiler: this.fb.array([]),
  });

  odgovorForm = this.fb.group({
    name: [null],
    quantity: [0, Validators.compose([Validators.required, Validators.min(0)])],
    total: 0,
  });

  constructor(private fb: FormBuilder) {}

  get articulo_alquiler() {
    return this.pitanjeForm.get("articulo_alquiler") as FormArray;
  }

  addArticulo(): void {        
    this.articulo_alquiler.push(this.odgovorForm);
    // console.log(this.articulo_alquiler);
    // console.log(this.pitanjeForm.value.articulo_alquiler);
  }
}

Comments

1

Replace Your code with below code

    public odgovori = this.pitanjeForm.controls['odgovori'];


 pitanjeForm = this.fb.group({
    data: ['', [Validators.required]],
    odgovori: this.fb.array([]),
    tacan: ['', [Validators.required]]
  });

  odgovorForm = this.fb.group({
    data: ['', [Validators.required]],
    color: 'danger'
  });

  addAnswer(): void {
    console.log(this.pitanjeForm.value, this.odgovorForm.value);
    this.info = this.odgovorForm.value;
    this.odgovori.push(this.info);
    console.log(this.odgovori)
  }

2 Comments

this code is little messy but i tried something like that, again same errror TypeError: control.setParent is not a function
odgovori is a FormArray, so you need push a FormControl (or a FormGroup) not a value: this.odgovori.push({...this.odgovorForm}) or this.odgovori.push(new FormGroup({data:new FormGroup('',Validators.required),color:new FormControl('danger')})
1

try using public odgovori = this.pitanjeForm.get('odgovori') as FormArray. Then angular knows it is an array and you can push something to it.

Comments

0

For Angular 8

This Worked for me

get formData() {
  return this.formData.get("names") as FormArray;
}

updateFormDataNames(newValue: any[]) {
  // Clear existing form array
  while (this.formDataNames.length !== 0) {
   this.formDataNames.removeAt(0);
  }

  // Populate the form array with new values
  newValue.forEach((item) => {
    this.nxpEntryFormEntries.push(new FormControl(item));
  });
}

Now you can easily pass a new array to updateFormDataNames(['1', '2'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.