简体   繁体   English

如何使用角度反应形式将数据正确绑定到单选按钮?

[英]how to correctly bind data to radio buttons using angular reactive forms?

I have struggled to do data biding to radio buttons using reactive forms.我一直在努力使用反应形式对单选按钮进行数据投标。

My html file is as below我的 html 文件如下

<form [formGroup]="newserviceform" (ngSubmit)="AddNewServices()" class="form" novalidate>
  <div class=" form-group row">
    <div class="col-sm-3 col-xs-12">
      <p class="model-input-lables">Type(per)*:</p>
    </div>
    <div class="col-xs-12 col-sm-9">
      <label class="radio-inline">
        <input class="radio-icon" type="radio" value=1 (focus)="focusNewServiceForm()" name="packagePriceOptionType"
          formControlName="packagePriceOptionType" checked>
        Event
      </label>
      <label class="radio-inline">
        <input class="radio-icon" type="radio" value=2 (focus)="focusNewServiceForm()" name="packagePriceOptionType"
          formControlName="packagePriceOptionType">
        Person
      </label>
      <label class="radio-inline">
        <input class="radio-icon" type="radio" value=3 (focus)="focusNewServiceForm()" name="packagePriceOptionType"
          formControlName="packagePriceOptionType">
        Item
      </label>
      <div
        *ngIf="packagePriceOptionType.errors && ((packagePriceOptionType.dirty && packagePriceOptionType.touched)||isformsubited)">
        <p class="form-error-messege" *ngIf="packagePriceOptionType.errors.required">Price option is required</p>
      </div>
    </div>
  </div>

  <div *ngIf="packagePriceOptionType.value!=1" class=" form-group row">
    <div class="col-sm-3 col-xs-12">
      <p class="model-input-lables">Quntity*:</p>
    </div>
    <div class="col-xs-12 col-sm-9">
      <!-- onkeydown is an inbuilt function to trigger an instatant key press 
    here it reduce taking the value of e and - in a key press -->
      <input (focus)="focusNewServiceForm()" type="number" min="200" class="form-control input-popup"
        formControlName="packageItemQuntity" (keyup)="numericOnly($event)"
        onkeydown="return event.keyCode !== 69 && event.keyCode !== 189"
        onkeypress="return event.charCode >= 48 && event.charCode <= 57">
      <div
        *ngIf="packageItemQuntity.errors && ((packageItemQuntity.dirty && packageItemQuntity.touched)||isformsubited)">
        <p class="form-error-messege" *ngIf="packageItemQuntity.errors.forbiddenValueValidator">Invalid value</p>
        <p class="form-error-messege" *ngIf="packageItemQuntity.errors.required">Quntity is required</p>
        <p class="form-error-messege" *ngIf="packageItemQuntity.errors.min">Minimum quantity is 200 </p>
      </div>
    </div>
  </div>
</form>

my ts file is as below.我的ts文件如下。 I am setting the initial value of the radio button to 1 .我将单选按钮的初始值设置为1 But it does not bind.但它不绑定。 But I saw that this can be done by using template driven forms.但我看到这可以通过使用模板驱动的表单来完成。 But here i need to do this using reactive forms但在这里我需要使用反应形式来做到这一点

  newserviceform: FormGroup;
  packagePriceOptionType: FormControl;
  packageItemQuntity: FormControl;

  ngOnInit() {
    this.createNewServiceForm();
  }

  createNewServiceForm() {
    this.createNewServiceFormControls();
    this.newserviceform = new FormGroup({
      packagePriceOptionType: this.packagePriceOptionType,
      packageItemQuntity: this.packageItemQuntity,
    });
  }

  createNewServiceFormControls() {
    this.packagePriceOptionType = new FormControl(1, [Validators.required]);
    this.packageItemQuntity = new FormControl(1, [
      Validators.min(1),
      this.forbiddenValueValidator(/^\d+(?:\.\d{1,2})?$/),
    ]);
  }

Do anyone have an idea where is the issue?有谁知道问题出在哪里? Thanks!!谢谢!!

I have checked your code and the issue is in assigning value attribute in template.我已经检查了您的代码,问题在于在模板中分配 value 属性。 When you are writing value="1" , it assign value of string "1" to value attribute.当您编写value="1" ,它将string "1" 的值分配给 value 属性。 But you are using number data type in reactive form.但是您正在以反应形式使用number数据类型。

If you want it to work perfectly use string type in your component class like done below:如果您希望它完美地工作,请在组件类中使用字符串类型,如下所示:

this.packageItemQuntity = new FormControl('1', [
      Validators.min(1),
      this.forbiddenValueValidator(/^\d+(?:\.\d{1,2})?$/),
    ]);

Please find the working stackblitz here - https://stackblitz.com/edit/angular-ynziux请在此处找到有效的stackblitz - https://stackblitz.com/edit/angular-ynziux

Somehow even if we provide numeric value to the value parameter in the input radio element, it is processed as strings.不知何故,即使我们为输入单选元素中的 value 参数提供数值,它也会作为字符串处理。 You can this instead(eg. [value]=1), works like a charm.你可以用这个来代替(例如 [value]=1),就像一个魅力。 eg.例如。

<input class="radio-icon" type="radio" [value=1] (focus)="focusNewServiceForm()" name="packagePriceOptionType" formControlName="packagePriceOptionType" checked>

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

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