angular checkbox 使用 ng-click 重置绑定到复选框的 ng-model,从而防止复选框被取消选中 [英] angular checkbox using ng-click to reset the ng-model that is binded to the checkbox, thus prevent the checkbox from being unchecked
问题描述
我有一些逻辑,在某些情况下,我希望始终选中第一个复选框.因此,如果用户尝试取消选中它,我想使用 ng-click 将绑定到复选框的 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 是真的...但显然我在这里错过了一些东西
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 是根据复选框的状态设置的:选中或未选中.所以它更像是一种绑定方式:根据复选框状态绑定 ng-model?
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
这是一个演示
这篇关于angular checkbox 使用 ng-click 重置绑定到复选框的 ng-model,从而防止复选框被取消选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!