简体   繁体   English

如何以 Angular Reactive 形式获取复选框值

[英]How to fetch checkbox value in Angular Reactive form

I am learning Angular now.我现在正在学习 Angular。 I am making a Reactive Form which has a group of checkboxes.我正在制作一个具有一组复选框的反应式表单。 I have created those checkboxes using Angular Material.我使用 Angular Material 创建了这些复选框。 So far after googling for the whole day I wasn't able to come up with a solution which tells me to get the checkbox checked value in FormGroup in TS file.到目前为止,在谷歌搜索了一整天之后,我无法想出一个解决方案,告诉我在 TS 文件中的FormGroup中获取复选框选中的值。 I am attaching the code herewith.我在此附上代码。

app.component.html应用程序组件.html

<form [formGroup]="formdata">
   <label>Vehicles</label><br>
   <mat-radio-group>
      <mat-checkbox *ngFor="let v of Vehicles" formControlName="vehicles" [value]="v.value">{{ v.option }}</mat-checkbox>
   </mat-radio-group>
</form>
{{ formdata.value | json }}

app.component.ts app.component.ts

import { FormBuilder } from '@angular/forms';

export interface model
{
  value: string; //Store value for the option
  option: string; //Stores option to be displayed
}

export class FormComponent implements OnInit{
    Vehicles: model[] = [{value: '2 wheelers', option: '2 wheelers'}, {value: '4 wheelers', option: '4 wheelers'}];

    constructor(private fb: FormBuilder) {}
formdata = this.fb.group({
    vehicles: []
});
}

I am getting the output as true or false`` but I want the output as 2 or 4 or 2,4```.我得到的输出为true or false`` but I want the output as 2 或 4 或 2,4```。

Please help me out.请帮帮我。

well, you can use the mat-checkbox alone.好吧,您可以单独使用 mat-checkbox。 Remember, the FormGroup exist even you has no input.请记住,即使您没有输入,FormGroup 也存在。 So you can use [checked] and (change) to change the value of the formControl.所以你可以使用[checked]和(change)来改变formControl的值。

<form [formGroup]="formdata">
   <label>Vehicles</label><br>
   <mat-radio-group>
      <mat-checkbox  
      (change)="formdata.get('vehicles').setValue($event.checked?2:null)"
      [checked]="formdata.value.vehicles==2" >2 Wheelers</mat-checkbox>
      <mat-checkbox 
      [checked]="formdata.value.vehicles==4"
      (change)="formdata.get('vehicles').setValue($event.checked?4:null)"

      >4 Wheelers</mat-checkbox>
   </mat-radio-group>
</form>

See stackblitz堆栈闪电战

Update really I don't understand the question, The question was create a serie of checkbox and return an array.更新我真的不明白这个问题,问题是创建一系列复选框并返回一个数组。 The idea it's the same, we has an array (in this case of object) and a FormControl.想法是一样的,我们有一个数组(在这种情况下是对象)和一个 FormControl。 we can use the own array of objects我们可以使用自己的对象数组

So,所以,

<form [formGroup]="formdata">
   <label>Vehicles</label><br>
   <mat-radio-group>
      <mat-checkbox *ngFor="let v of Vehicles" 
         [checked]="v.active" 
         (change)="v.active=$event.checked;setVehicles()">
          {{ v.option }}
       </mat-checkbox>
   </mat-radio-group>
</form>

And the function "setVehicles" is the function that give value to the form.而函数“setVehicles”是赋予表单价值的函数。 How?如何? filter the array Vehicles and map to the value过滤数组 Vehicles 并映射到值

setVehicles()
  {
    this.formdata.get('vehicles').setValue(
       this.Vehicles.filter(x=>x.active) //first filter
       .map(x=>x.value)  //second map to get only the "value"
    )
  }

See the new stackblitz查看新的堆栈闪电战

NOTE: I use the same array of objects and add a property "active" "on fly" (this is because I defined the array as :any[])注意:我使用相同的对象数组并添加了一个“动态”属性“动态”(这是因为我将数组定义为 :any[])

NOTE2: In this case I give value to the formControl each time you change any check-box, this action can be used only when we submit the form注意2:在这种情况下,每次更改任何复选框时我都会为 formControl 赋值,只有在我们提交表单时才能使用此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM