我有2个单选按钮,我正在使用反应式表单,我在组件中添加了表单控件.我面临的问题是name属性必须与formControlName相同.当我将name属性设置为相同时,我只能选择1个单选按钮 - 永远不能取消选择并选择另一个.只允许我选择同一个.
this.genderControl = new FormControl("", Validators.required);
Run Code Online (Sandbox Code Playgroud)
然后添加到我的表格组
genderControl: this.genderControl,
Run Code Online (Sandbox Code Playgroud)
我的HTML:
<div class="radio-inline">
<input id="gender" type="radio" name="genderControl" formControlName="genderControl" />
<label class="radio-label"> Male</label>
<input id="gender" type="radio" name="genderControl" formControlName="genderControl" />
<label class="radio-label">Female</label>
</div>
Run Code Online (Sandbox Code Playgroud)
表格组
this.personalInfo = new FormGroup({
searchControl: this.searchControl,
titleControl: this.titleControl,
firstNameControl: this.firstNameControl,
middleNameControl: this.middleNameControl,
lastNameControl: this.lastNameControl,
birthdayControl: this.birthdayControl,
genderControl: this.genderControl,
phoneControl: this.phoneControl,
taxCanadaControl: this.taxCanadaControl,
provinceControl: this.provinceControl,
countryControl: this.countryControl,
taxCountryControl: this.taxCountryControl,
creditControl: this.creditControl
});
Run Code Online (Sandbox Code Playgroud)