0

I have a Array and any input, I would like when submit get all values this array in the inputs

<div *ngIf="this.exercises.length != 0" >
  <div class="row" *ngFor="let exercise of exercises; let i = index">
    <div class="col lg-3">
      {{ exercise.name }}
    </div>
    <div class="col lg-3">
      <input matInput type="text" placeholder="0" formControlName="serie">
    </div>
    <div class="col lg-3">
      <input matInput type="text" placeholder="0" formControlName="repetition"> {{i}}
    </div>
    <div class="col lg-3">
      <input matInput type="text" placeholder="0" formControlName="weigth">{{i}}
    </div>
  </div>
</div>

how can get all values in the inputs ?

enter image description here

6
  • Your best bet is to look into FormArray. This allows a dynamic number of FormControl to be captured. Commented Sep 7, 2019 at 12:50
  • Similar to this one angular.io/api/forms/FormArrayName ? Commented Sep 7, 2019 at 12:52
  • Your requirements seems to be unclear. Do you want to show array values in input? Can you not use value attribute on input to show ? Commented Sep 7, 2019 at 12:52
  • Have a look at this guide: angular.io/guide/… Commented Sep 7, 2019 at 12:53
  • yes each array item has a entry Commented Sep 7, 2019 at 12:55

2 Answers 2

1

You can use FormGroup like that

<div *ngIf="this.exercises.length != 0" >
  <form [formGroup]="itemForm" >
  <div class="row" *ngFor="let exercise of exercises; let i = index">
    <div class="col lg-3">
      {{ exercise.name }}
    </div>
    <div class="col lg-3">
      <input matInput type="text" placeholder="0" formControlName="serie{{i}}">
    </div>
    <div class="col lg-3">
      <input matInput type="text" placeholder="0" formControlName="repetitionserie{{i}}"> {{i}}
    </div>
    <div class="col lg-3">
      <input matInput type="text" placeholder="0" formControlName="weigthserie{{i}}">{{i}}
    </div>
  </div>
  </form>
</div>

Secondly,add property for FormGroup name as itemForm and whenever you want in your ts file call the value of form like

itemForm: FormGroup;
...

ngOnInit(){
  let index=0;
  this.exercices.forEach(controlName =>
    this.itemForm.addControl('serie' + index, new FormControl(''));
    this.itemForm.addControl('repetitionserie' + index, new FormControl(''));
    this.itemForm.addControl('weigthserie' + index, new FormControl(''));
    index++;
  );
}

onSubmit(){
   let formValue = this.itemForm.value;
   ....
}
Sign up to request clarification or add additional context in comments.

7 Comments

I get this error for all inputs ROR Error: Cannot find control with name: 'serie0' ... ?
I'm sorry friend, but I'm use this way this.form = this.fb.group({ name: [ '', Validators.required ], serie: [ '', Validators.required ], repetition: [ '', Validators.required ], weigth: [ '', Validators.required ], });
can I implement this way ?
Yes you can implement your form builder
@ahmetical, why don't use a FormArray?
|
0

Rafael, think in FormArray

First create a function that return a formGroup, and a variable that was a FormArray

formArray:FormArray;

createGroup(data:any)
{
   data=data || {serie:0,repetition:0,weigth:0}
   return new FormGroup({
       serie:new FormControl(data.serie),
       repetition:new FormControl(data.repetition),
       weigth:new FormControl(data.weigth),
   }))
}

second in a ngOnInit create the formArray using your array exercises. You can loop over exercises and use push or use map

ngOnInit()
{
    this.formArray=new FormArray(this.exercises.map(x=>{
        return this.createGroup(null)
    })
}

You simple iterate over FormArray.controls.(*)

<form *ngIf="formArray" [formGroup]="formArray" (submit)="submit(formArray)">
    <div *ngFor="let group of formArray.controls;let i=index" [formGroup]="group">
        {{exercises[i]}}
        <input formControlName="serie">
        <input formControlName="repetition">
        <input formControlName="weigth">
    </div>
    <button type="submit">submit</button>
</form>

You can see the code in stackblitz

(*)Yes, there're severals ways to iterate over a FormArray. The clasic is a FormArray in a FormGroup and we use

<form [formGroup]="formGroup">
  <div formArrayName="formArray">
      <div *ngFor="let group of formGroup.get('formArray').controls; let i = index;"
           [formGroupName]="i">
           <input formControlName="...">
           <input formControlName="...">
             ...
      </div>
  </div>
</form>

But if we has only a formArray, we can simply loop over formArray.controls and indicate that the [formGroup] is the variable over we iterate. We are replacing [formGroupName]="i" by [formGroup]="group"

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.