javascript AngularJS 复选框选中自定义值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24947669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS checkbox checked custom value
提问by Sethe23
I'm trying to have a box checked by default with a custom value.
我正在尝试使用自定义值默认选中一个框。
Are you ready? <input type="checkbox" ng-model="checked" ng-init="checked=true" ng-true-value="sure" ng-false-value="nope"><br/>
<tt> {{checked}}</tt><br/>
If I set it to that nothing displays until I click the box. Is there a way to where it can be checked with a default value being something other than a boolean value?
如果我将其设置为在单击该框之前不会显示任何内容。有没有办法用默认值不是布尔值来检查它?
回答by hutingung
ng-true-value="sure"
. So, ng-init="checked='sure'"
ng-true-value="sure"
. 所以,ng-init="checked='sure'"
Code Snippet
代码片段
<input type="checkbox" ng-model="checked" ng-init="checked='sure'" ng-true-value="sure" ng-false-value="nope">
Plunkr - http://plnkr.co/edit/ejYSsyIjMZSZua9cQyv1?p=preview
Plunkr - http://plnkr.co/edit/ejYSsyIjMZSZua9cQyv1?p=preview