how do i bind output values of another component to a form-control in my component

牧云@^-^@ 提交于 2019-12-13 04:39:23

问题


This is my html code which is basically a form.

<form [formGroup]="calculatedForm" fxLayout="column" fxLayoutGap="15px" fxLayoutAlign="start space-betwen">
    <label for="category">CATEGORY</label>
      <app-toggle-button-group
        [items]="categories"
        [selectedItem]="getselectedCategory()"
        (valueChange)="updateCategory($event)"
        [disableButtons]="calculatedForm.value.system">
      </app-toggle-button-group>

      <label for="input_interval">INTERVAL</label>
      <app-toggle-button-group
        [items]="inputIntervals"
        [selectedItem]="getselectedInterval()"
        (valueChange)="updateInterval($event)"
        [disableButtons]="calculatedForm.value.system">
      </app-toggle-button-group>

      <label for="aggregation">AGGREGATION</label>
      <app-toggle-button-group
        [items]="aggregations"
        [selectedItem]="getselectedAggregation()"
        (valueChange)="updateAggregation($event)"
        [disableButtons]="calculatedForm.value.system">
      </app-toggle-button-group>

      <app-kpi-unit 
        [selectedUnitID]="selectedUnitId()" 
        (changedUnitID)= "selectUnit($event)"
        [disableSelect]="calculatedForm.value.system">
      </app-kpi-unit>

    </form>

this is my form property in controller

  calculatedForm = this.formBuilder.group({
    id: new FormControl(''),
    identifier: new FormControl(''),
    category: new FormControl('', [Validators.required]),
    aggregation: new FormControl(null, [Validators.required]),
    input_interval: new FormControl('', [Validators.required]),
    operator: new FormControl('', [Validators.required]),
    unit_id: new FormControl(null),
    org_unit: new FormControl('', [Validators.required]),
    system: new FormControl(false),
    deleted: new FormControl(false),
    assignedKpiId: new FormControl(null)
  });

As you can see in html code I am using a method from event emitted by components to update my form controls

updateCategory(category: buttonGroupType) {
    this.calculatedForm.controls['category'].patchValue(category.name);
  }

but my requirement is to bind using formcontrolName instead of using the methods. is it possible to avoid the update methods


回答1:


why aren't you using formControl?

this can bind your Html to form controls <input [formControl]="category"></input>



来源:https://stackoverflow.com/questions/58100248/how-do-i-bind-output-values-of-another-component-to-a-form-control-in-my-compone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!