0

Please help, I have collection in angular component:

 collection: collectionAbs[];
    
 export interface collectionAbs{
   name: string;
   prop: string;
   secondProp: number;
 }

And I initialize:

    this.collection.forEach((item ,index) => {
      formGroup.addControl(index.toString() + "-prop", new FormControl('', [Validators.required])),
      formGroup.addControl(index.toString() + "-secondProp", new FormControl('', [Validators.required]))
    });

Object is initiated and I want to update properties in form. I have something like this:

  <div *ngFor="let item of collection; let i = index" >
         <input type="text" name="collection[{{i}}].prop">
         <input type="number" name="collection[{{i}}].secondProp">
  </div>

But I haven't no idea how to assign now this values to my objects collection. Maybe someone have better solution for this problem?

@Edit

FormGroup:

 var formGroup: FormGroup;

 formGroup = new FormBuilder().group({
   anotherFormInputOne: new FormControl(null, [Validators.required]),
   anotherFormInputTwo: new FormControl(null, [Validators.required])
 });

Thanks!

4
  • Of course, I edited post. Commented Jan 11, 2021 at 18:16
  • Not exactly, I have a collection already created and I want to display the inputs for property for all objects. I just want to add a value to the property for each element. Commented Jan 11, 2021 at 18:31
  • I don`t know how to bind this input value to my object Commented Jan 11, 2021 at 18:32
  • You should check how to use Reactive forms Commented Jan 11, 2021 at 19:07

1 Answer 1

1

it should just be a case on submit of the form of doing:

    this.collection.forEach((item ,index) => {
      item.prop = formGroup.controls[index.toString() + "-prop"].value;
      item.secondProp = formGroup.controls[index.toString() + "-secondProp"].value;
    });
Sign up to request clarification or add additional context in comments.

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.