简体   繁体   English

Angular2:具有单选按钮功能的复选框

[英]Angular2: Checkbox with radio button functionality

Can I get a guidance in implementing radio button functionality in checkbox using angular2 ? 我可以获取有关使用angular2在复选框中实现单选按钮功能的指南吗? I tried with same name and ngModel, but not working 我尝试使用相同的名称和ngModel,但无法正常工作

Here is my component.html 这是我的component.html

<form [formGroup]="conformityForm">     
   <table class="table table-bordered">
      <thead>
         <tr>
           <th>Evaluation</th>
           <th>Yes</th>
           <th>No</th>
         </tr>
      </thead>
      <tbody>
        <tr>
          <td>Do the staff have adequate qualifications for the tasks?</td>
          <td>
             <input type="checkbox" formControlName="IsAdequatequalificationsYes">
          </td>
          <td>
             <input type="checkbox" formControlName="IsAdequatequalificationsNo">
          </td>
        </tr>
      </tbody>
    </table>
 </form>

component.ts component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';

  export class Test implements OnInit {
   conformityForm: FormGroup;
    constructor(private formBuilder: FormBuilder)      
      this.conformityForm = this.formBuilder.group({
        'IsInConvenienceYes': [''],
        'IsInConvenienceNo': ['']
     });
  }
}

EDIT: 编辑:

I wasn't really thinking this true, so you did not want to disable the field when other checkbox is checked. 我并不是真的这么认为,因此您不想在选中其他复选框时禁用该字段。 But here is the solution for the radio button behavior. 但是,这是单选按钮行为的解决方案。 We are using the same variables yes and no , like below. 我们正在使用相同的变量yesno ,如下所示。 Here we just patch the values with false and true that will uncheck the other if the other is checked. 在这里,我们仅使用falsetrue修补值,如果选中另一个则将取消选中另一个。 So change your template to this: 因此,将模板更改为此:

<input type="checkbox" (change)="yes.patchValue(true) ? yes.patchValue(true) : no.patchValue(false)" formControlName="IsAdequatequalificationsYes">
<input type="checkbox" (change)="no.patchValue(true) ? no.patchValue(false) : yes.patchValue(false)" formControlName="IsAdequatequalificationsNo">

PLUNKER PLUNKER


ORIGINAL ANSWER: 原始答案:

As a first comment I can say that the usual [disabled] doesn't work with reactive forms, so we need to do it in another way. 首先,我可以说通常的[disabled]不适用于反应形式,因此我们需要以另一种方式来实现。

I like to shorten the form controls for better readability, here I have used variables yes and no , you maybe want to do some more suitable names? 我想缩短表单控件以提高可读性,在这里我使用了yesno变量,您可能想使用一些更合适的名称? Anyway, they are defined in the TS like so: 无论如何,它们在TS中的定义如下:

this.yes = this.conformityForm.get('IsAdequatequalificationsYes');
this.no = this.conformityForm.get('IsAdequatequalificationsNo');

Then we add a change event to the form, where we check if the current checkbox is checked, if so, we disable the other one, and the other way around using the ternary operator. 然后,我们向表单中添加一个change事件,在其中检查是否选中了当前复选框,如果是,则禁用另一个复选框,反之则使用三元运算符。 So do the following for the yes : 因此对“ yes执行以下操作:

<input type="checkbox" (change)="yes.value ? no.disable() : no.enable()" formControlName="IsAdequatequalificationsYes">

and for the no we just change it to the opposite. no我们只是将其改为相反。

That should do it! 那应该做! :) :)

PLUNKER PLUNKER

Also, you can use this option: 另外,您可以使用以下选项:

 <input type="checkbox" (change)="yes.patchValue(true); no.patchValue(false);" formControlName="IsAdequatequalificationsYes"> <input type="checkbox" (change)="yes.patchValue(false); no.patchValue(true);" formControlName="IsAdequatequalificationsNo"> 

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

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