这是我的代码,在姓氏中,我使用了 ngModel,因此它可以进行双向数据绑定(bind),但在单选按钮中,我如何使用 ngModel 进行双向数据绑定(bind)。
<div class="form-group" [ngClass]="{ 'has-error': f.submitted && !lastname.valid }">
<label for="lastname">Last Name</label>
<input type="text" class="form-control" name="last_name" [(ngModel)]="employee.last_name" #lastname="ngModel" required />
<div *ngIf="f.submitted && !lastname.valid" class="help-block">Last Name is required</div>
</div>
<div class="form-group">
<label>Gender:</label>
<label class="radio-inline">
<input type="radio" name="optradio" [checked]="employee.gender == 'Male'" >Male
</label>
<label class="radio-inline">
<input type="radio" name="optradio" [checked]="employee.gender == 'Female'" >Female
</label>
</div>
最佳答案
对于双向数据绑定(bind),它与所有其他方式一样使用此语法 [(ngModel)]
将您的代码块替换为以下内容:
<div class="form-group">
<label>Gender:</label>
<label class="radio-inline">
<input type="radio" name="optradio" value='Male' [(ngModel)]="employee.gender" >Male
</label>
<label class="radio-inline">
<input type="radio" name="optradio" value='Female' [(ngModel)]="employee.gender" >Female
</label>
</div>
关于angular - 如何在 Angular 2中绑定(bind)双向单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45275792/