选中设置1时取消选中angular2复选框值,取消选中设置为0

时间:2017-03-20 10:59:29

标签: javascript angular typescript

我想使用选中和未选中

来检查值0或1



<div class="roadblock"> <input type="checkbox" class="checkbox_style1" id="roaddiv" [(ngModel)]="note.selected" /> </div>
<div class="roadtext"> Road block </div>
<div class="roadblock"> <input type="checkbox" class="checkbox_style1" id="devdiv" [(ngModel)]="note.selected" /> </div>
<div class="roadtext"> Deviation </div>
<div class="roadblock"> <input type="checkbox" class="checkbox_style1" id="changediv" [(ngModel)]="note.selected" /> </div>
<div class="roadtext"> Change in approach</div>
<div class="roadblock"> <input type="checkbox" class="checkbox_style1" id="techdiv" [(ngModel)]="note.selected" /> </div>
<div class="roadtext"> Tech help</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:6)

您可以使用ngModel绑定中的表达式来翻译值:

[ngModel]="note.selected == 1 ? true : note.selected == 0 ? false : null" (ngModelChange)="note.selected = $event ? 1 : 0"

但这需要将[(ngModel)]分为[ngModel](ngModelChange)