angular复选框使用ng单击重置绑定到复选框的ng模型,从而防止复选框被取消选中 [英] angular checkbox using ng-click to reset the ng-model that is binded to the checkbox, thus prevent the checkbox from being unchecked
问题描述
我有一些逻辑,在某种情况下,我想第一个复选框被检查。因此,如果用户尝试取消选中它,我想使用ng单击将绑定到复选框的ng-model更改为true。但是复选框仍然被取消选中....
I have some logic that on certain senario, I want the first check box to be alway checked. So if user tries to uncheck it, I want to use ng-click to change the ng-model binded to the checkbox to 'true'. But the checkbox is still being unchecked....
如何实现保持复选框保持检查基于ng-model的值,而不使用像angular.element (elem).attr(checked,true)
How do I achieve keep the checkbox remains checked based on the ng-model's value, without using something like angular.element(elem).attr("checked", true)
<input type = 'checkbox' ng-model = 'checkboxValue' ng-click = "handler"/>
$scope.checkboxValue = false;
$scope.handler = function(){
$scope.checkboxValue = true;
}
我希望复选框保持选中状态,因为ng-model checkboxValue为true。显然我错过了这里的东西。
I want the checkbox remain checked since the ng-model checkboxValue is true...but apparently I missed something here
这里是plunker: http://plnkr.co/edit/WbjZilFLDfbP3mQ4oMgx?p=preview
Here's the plunker:http://plnkr.co/edit/WbjZilFLDfbP3mQ4oMgx?p=preview
我走进了调用栈,看起来像我设置ng -model为true,然后在$ digest期间,ng-model根据复选框的状态设置:选中或取消选中。所以它更像是单向绑定:根据checkbox状态绑定ng模型?
I stepped into the call stack, looks like I set the ng-model to true, then during $digest, the ng-model is set based on the checkbox's status : checked or unchecked. So it is more like one way binding: bindding the ng-model based on the checkbox status?
推荐答案
只需使用 ng-change
,而不是 ng-click
这里是 DEMO
这篇关于angular复选框使用ng单击重置绑定到复选框的ng模型,从而防止复选框被取消选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!