简体   繁体   中英

angular2 checkbox value when checked set 1 and unchecked to set 0

I want to check value whether 0 or 1 using checked and unchecked

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

You can use expressions in ngModel bindings to translate the values:

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

but this requires to split [(ngModel)] to [ngModel] and (ngModelChange)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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