将复选框值传递给angular的ng-click [英] Pass checkbox value to angular's ng-click
问题描述
是否可以通过ng-click关联输入的值传递给角度指令?
Is there a way to pass to angular directive ng-click the value of the associated input?
换句话说,在以下情况下应替换 this.value
的内容:
In other words, what should replace this.value
in the following:
<input type="checkbox" id="cb1" ng-click="checkAll(this.value)" />
PS.我不需要替代值的解决方法,我只是想知道是否有可能将输入的值作为角度函数的参数传递.
PS. I don't want a workaround to alternate values, I just wonder if is possible to pass a value of an input as argument to an angular function.
推荐答案
您可以执行以下操作:
- 如果需要,可以使用 ng-model 将html组件绑定到角度范围
- 将
ng-click
更改为ng-change
- 如果必须在选中和取消选中复选框时绑定一些值,请使用
ng-true-value="someValue"
和ng-false-value="someValue"
- Use ng-model if you want to bind a html component to angular scope
- Change
ng-click
tong-change
- If you have to bind some value upon checking and un-checking the checkbox use
ng-true-value="someValue"
andng-false-value="someValue"
ng-click
和ng-model
的执行顺序不明确,因为 他们没有定义明确的优先级.相反,您应该使用ng-change 或$scope
上的$watch
以确保获得正确的值 的模型变量.
The order of execution of
ng-click
andng-model
is ambiguous since they do not define clear priorities. Instead you should use ng-change or a$watch
on the$scope
to ensure that you obtain the correct values of the model variable.
Working Demo
<input type="checkbox" id="cb1" ng-model="check" ng-change="checkAll(check)" ng-true-value="YES" ng-false-value="NO"/> Citizen
这篇关于将复选框值传递给angular的ng-click的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!