Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
how can i set a value for property

I am working on an Angular application using the reactive form. I get the value of the fields using the form group but those fields are created dynamically (the user controls the number of rows of fields). so for that, I have done the following code :

this.formSaisieSecondary = this.fb.group({
  options:this.fb.array([])
});

newOption(): FormGroup {  
return this.fb.group({  
  date:['', Validators.required],

  cargoA:['', Validators.required],
  cargoB:['', Validators.required],
  cargoC:['', Validators.required],
  cargoD:['', Validators.required],
  cargoE:['', Validators.required],

  unite:[this.unite, Validators.required],

  siloA:['silo1', Validators.required],
  siloB:['silo2', Validators.required],
  siloC:['silo3', Validators.required],
  siloD:['silo4', Validators.required],
  siloE:['silo5', Validators.required],

  comment:[''] 
})  
}

options() : FormArray {  
   return this.formSaisie.get("options") as FormArray ;
}
 
AddOption(){ 
   this.options().push(this.newOption()); 
}

the HTML code :

<form [formGroup]="formSaisie">
        <div formArrayName="options">
            <div class="row mb-5" style="height: 23px;" *ngFor="let option of options().controls; let i = index" [formGroupName]="i">
                <div class="col-md-1">
                    <input formControlName="date" type="text" formControlName="date" hidden [(ngModel)]="listDate[i]">
                    <p>{{listDate[i]}}</p>
                </div>
                <div class="col-md-2">
                    <mat-form-field class="inputGridItem" appearance="fill">
                        <mat-label>Cargaison</mat-label>
                        <mat-select formControlName = "cargoA" required>
                            <mat-option *ngFor="let cargo of dataEx" [value]="cargo.cargoId">
                                {{cargo.vessel +" "+ cargo.externalId}}
                            </mat-option>
                        </mat-select>
                    </mat-form-field>
                </div>
 .............

the code is inspired by the following the enter method

my question here is how I can get or set a value for a property(field) for example for getting a value I can do :

let value = this.formSaisie.value.options[0];
value.cargoA;

but I can't set a value for afield.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do it by for example this.formSaisie.get('cargoA').setValue(someValueYouWouldLikeToSet)

edit: So do it like: this.formSaisie.get('options').get('cargoA').setValue(someValue) - should work

about 4 years ago · Juan Pablo Isaza Report

0

this.options().at(0).get('cargoA').setValue(someValue)

This sets the cargoA property of the group at index 0 in the form array to someValue

Side note: strongly recommend to not use ngModel and reactive forms in the same form. They will conflict with each other and be unpredictable at times.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!