Angular formArray radio buttons

前端 未结 1 1575
死守一世寂寞
死守一世寂寞 2021-01-24 18:33

I am attempting to add a radio button group to a FormArray. The issue is that when I select a value, it changes the value for every member of the FormArray. I know this has to d

相关标签:
1条回答
  • 2021-01-24 18:35
    <div *ngIf="sensorForm"  [formGroup]="sensorForm" (ngSubmit)="createSensor()>
      <!---first the name of array-->
      <div formArrayName="definitions">
          <!--after the loop using a div, this div has [fromGroupName]=i -->
          <div *ngFor="let definition of sensorForm.get('definitions').controls; 
                      let i = index;" [formGroupName]="i">
              <!--each element, only use formControlName, ¡NOT! [(ngModel)] -->
              <!-- ngModel is for Template driven Form  -->
              <input type="radio" [value]="true" formControlName="numeric" > Yes
              <input type="radio" [value]="false" formControlName="numeric" > No
          </div>
      </div>  
    </div>
    

    Your addDefinition is wrong, you have not "this.definitions"

    addDefinition() {
        const definitions = this.sensorForm.get('definitions') as FormArray;
        definitions.push(this.createDefinition());
      }
    
    0 讨论(0)
提交回复
热议问题