将复选框值传递给 angular 的 ng-click

Pass checkbox value to angular's ng-click

有没有办法传递给 angular 指令 ng-click 关联输入的值?

换句话说,应该用什么替换下面的this.value

<input type="checkbox" id="cb1" ng-click="checkAll(this.value)" />

PS. 我不想要替代值的解决方法,我只是想知道是否可以 传递输入值作为 angular 函数的参数.

为其分配一个 ng-model 将 return 布尔值,具体取决于对其的更改:

<input type="checkbox" id="cb1" ng-model="checkValue" ng-change="checkAll(checkValue)" />

最好使用 ng-change 而不是 ng-click

将复选框绑定到一个值并将其传递给 ng-click

<input type="checkbox" ng-model="value" id="cb1" ng-click="checkAll(value)" />

您可以执行以下操作:

  • 如果要将 html 组件绑定到 angular 范围
  • ,请使用 ng-model
  • ng-click更改为ng-change
  • 如果您必须在选中和取消选中复选框时绑定一些值,请使用 ng-true-value="someValue"ng-false-value="someValue"

The order of execution of ng-click and ng-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

提供

Working Demo

<input type="checkbox" id="cb1" ng-model="check" ng-change="checkAll(check)" ng-true-value="YES" ng-false-value="NO"/> Citizen

今天我想做同样的事情,但我发现没有人回答实际问题。这违反了 Angular 理念,但您可以将 "this.value" 替换为 "$event.target.checked" :

<input type="checkbox" id="cb1" ng-click="checkAll($event.target.checked)" />

您可以执行以下操作。为我工作。

<input type="checkbox" id="cb1" ng-click="onChecked($event.target.checked)" />