AngularJS:NG-模式不绑定到NG-检查复选框 [英] AngularJS: ng-model not binding to ng-checked for checkboxes
问题描述
我提到了这个问这个问题之前。
I referred to this before asking this question.
<一个href=\"http://stackoverflow.com/questions/14226439/angularjs-doesnt-bind-ng-checked-with-ng-model\">AngularJs不绑定NG-检查与NG-模型
如果 NG-检查
进行评估,以<code>真正在 HTML
端, NG-模型
不更新。我不能 NG-重复
所建议的上述问题,因为我必须使用一些造型为每个复选框。
If ng-checked
is evaluated to true
on the html
side, the ng-model
is not updated. I can't ng-repeat
as suggested in the above question because I have to use some styling for each checkbox.
下面是我创造来说明我的问题plunker。
Here is the plunker that I have created to illustrate my problem.
http://plnkr.co/edit/YsOsPh3vjkPMUUDa6r2t
要看到我想要的东西,请打开控制台,只需点击提交
按钮。请不要检查任何复选框。
To see what I want, please open the console, and just click on Submit
button. Please don't check any checkboxes.
在此先感谢!
推荐答案
ngModel
和 ngChecked
并不意味着可以一起使用。
ngModel
and ngChecked
are not meant to be used together.
ngChecked
是说期待一个前pression,所以 NG-检查=真
,你基本上说,该复选框将始终默认进行检查。
ngChecked
is expecting an expression, so by saying ng-checked="true"
, you're basically saying that the checkbox will always be checked by default.
您应该能够只使用 ngModel
,拴在你的模型布尔属性。如果你想要别的东西,那么你要么需要使用 ngTrueValue
和 ngFalseValue
(只支持字符串现在),或者自己写指令。
You should be able to just use ngModel
, tied to a boolean property on your model. If you want something else, then you either need to use ngTrueValue
and ngFalseValue
(which only support strings right now), or write your own directive.
究竟是什么,你想干什么?如果你只是想在默认情况下进行检查的第一个复选框,你应该改变你的模型 - ITEM1:真,
What is it exactly that you're trying to do? If you just want the first checkbox to be checked by default, you should change your model -- item1: true,
.
编辑:您不必提交表单调试模型的当前状态,顺便说一句,你可以转储 {{} testModel}
到HTML (或&LT; pre&GT; {{testModel | JSON}}&LT; / pre&GT;
)。此外,您的 ngModel
属性可以简化为 NG-模式=testModel.item1
。
You don't have to submit your form to debug the current state of the model, btw, you can just dump {{testModel}}
into your HTML (or <pre>{{testModel|json}}</pre>
). Also your ngModel
attributes can be simplified to ng-model="testModel.item1"
.
<一个href=\"http://plnkr.co/edit/HtdOok8aieBjT5GFZOb3?p=$p$pview\">http://plnkr.co/edit/HtdOok8aieBjT5GFZOb3?p=$p$pview
这篇关于AngularJS:NG-模式不绑定到NG-检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!