gpt4 book ai didi

angular - 如何在 Angular 2中绑定(bind)双向单选按钮

转载 作者:太空狗 更新时间:2023-10-29 17:44:33 24 4
gpt4 key购买 nike

这是我的代码,在姓氏中,我使用了 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> &nbsp;
<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>
&nbsp;

<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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com