将复选框值传递给 angular 的 ng-click [英] Pass checkbox value to angular's ng-click
问题描述
有没有办法将相关输入的值传递给 angular 指令 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 组件绑定到 angular 作用域
- 将
ng-click
改为ng-change
- 如果您必须在选中和取消选中复选框时绑定一些值,请使用
ng-true-value="someValue"
和ng-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.
由 musically_ut 提供
<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屋!