0

I have a Reactive form that group select on the left and input type number on the right and I want to get a value that combines this two.

enter image description here

For example

dynamicForms.html

<form novalidate (ngSubmit)="onSubmit(form.value)" [formGroup]="form">

    <div fxLayout="row" fxLayoutGap="10px" *ngSwitchCase="'number'">

        <mat-form-field *ngIf="prop.units !== [];" fxFlex="80px">
          <mat-select placeholder="Unit" (change)="form.value.frequencies.unit = 'KHz';">
            <mat-option *ngFor="let unit of prop.units" [value]="unit">
              {{ unit }}
            </mat-option>
          </mat-select>
        </mat-form-field>

        <mat-form-field *ngSwitchCase="'number'" fxFlex="100" class="full_width">
          <input 
            type="number" 
            matInput 
            [formControlName]="prop.key"
            [placeholder]="prop.key" 
            [id]="prop.key"
            [min]="prop.min"
            [max]="prop.max"
            value="{{prop.value}}">
          <mat-icon *ngIf="prop.description" class="inp_description" matTooltip="{{prop.description}}" matTooltipShowDelay="500" matSuffix>help</mat-icon>
        </mat-form-field>

      </div>


</form>



   <p>
       <button mat-raised-button [disabled]="form.invalid" type="submit">Save</button>
   </p>

What I get is:

frequencies: 58

And what I want on submit is this values.

    frequencies: {
       value: 58,
       unit: 'KHz'
    }

I try every trick I know in Anguar but nothink is work

5
  • what did you try so far ? Commented Feb 5, 2018 at 11:02
  • I try everything I know sow far but I cant make that nested object Commented Feb 5, 2018 at 11:08
  • @JEY I try to separate the select from input and I want to know if is HZ, KHz or GHz Commented Feb 5, 2018 at 11:09
  • can you also show your .ts file? Commented Feb 5, 2018 at 11:15
  • Yes but you nead to know that this is dynamic forms with reactive forms Commented Feb 5, 2018 at 11:18

1 Answer 1

0

You can create group controls in the following way:

this.entryForm = new FormGroup({
  'frequencies': new FormGroup({
    'value': new FormControl(),
    'unit': new FormControl()
  })
})

In your html form add a div to group these controls, and change the path of formControlName in the fields.

<form [formGroup]="entryForm">
  <div formGroupName="frequencies">
    <input type="number" formControlName="value" />
    <select formControlName="unit">
      <option value="khz">Khz</option>
    </select>
  </div>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

Shrey thank you looks like this is the solution but when I try this I get that error ERROR Error: Cannot find control with path: 'frequencies -> '

Your Answer

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