问题描述
我想知道如何使用 Angular2 使复选框选中值是否为真,如果值为假则取消选中.
I would like to know how to make a checkbox checked if the value is true, and unchecked if false with Angular2.
Adult <input type="checkbox" value="{{person.is_adult}}">
{{person.is_adult}} 是一个 boolean
{{person.is_adult}} is a boolean
有人可以提出任何建议吗?谢谢
Can someone please suggest anything? Thanks
推荐答案
{{}} 做字符串插值和字符串化true和falseAngular 默认使用属性绑定,我假设属性需要布尔值而不是字符串:
{{}} does string interpolation and stringifies true and false and Angular by default uses property binding and I assume the property expects boolean values not strings:
<input type="checkbox" [checked]="person.is_adult">
这可能也有效
<input type="checkbox" attr.checked="{{person.is_adult}}">
因为使用属性绑定,浏览器可能会在将属性(只能是字符串)读取到其属性时将其转换为布尔值.
because with attribute binding the browser might translate it from the attribute (which can only be strings) to boolean when reading it into its property.
也是checked而不是value
你也可以使用ngModel
<input type="checkbox" [ngModel]"person.is_adult" name="isAdult">
<input type="checkbox" [(ngModel)]"person.is_adult" name="isAdult">
用于单向或双向绑定.
如果您使用 ngModel,请确保您已导入 FormsModule.
for one-way or two-way binding.
Ensure your have the FormsModule imported if you use ngModel.
这篇关于复选框检查了 Angular2 的布尔值是否为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!




大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)