Angular2是否可以使用带有复选框的双向绑定? [英] Angular2 is there a way to use a two way binding with a checkbox?
本文介绍了Angular2是否可以使用带有复选框的双向绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为复选框使用双向绑定,但是如果使用 [(ngModel)]
,它将显示true或false而不是选中的项目。有人知道怎么做吗?
I want to use a two way binding for a checkbox, but if I use the [(ngModel)]
it is showing true or false instead of the value of the checked item. Does anybody know how this is done?
如果我想使用双向绑定并将其设置为 expression.value,我该怎么做:
If i want to use a two way binding and set it to "expression.value" in my case how do i do that:
<input type="checkbox" type="checkbox"/> Option 1</a></li>
在这种情况下,我想将复选框的值绑定:选项1到表达式类中。
I would like to bind the value of the checkbox in this case: Option 1 into the expression class.
推荐答案
这是一个已知问题
- https://github.com/angular/angular/issues/3406,
- https://github.com/angular/angular/issues/6311
有不同的解决方法,例如使用 event.target.checked
而不是模型中的值。
There are different workarounds like using event.target.checked
instead of the value from the model.
您可以使用
<input type="checkbox"
(change)="expression && expression.Option1=$event.target.checked ? true : undefiend"
[ngModel]="expression?.Option1">
<input type="checkbox"
(change)="expression && expression.Option2=$event.target.checked ? true : undefiend"
[ngModel]="expression?.Option2">
这篇关于Angular2是否可以使用带有复选框的双向绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文