Javascript 将复选框值传递给 angular 的 ng-click
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33431591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass checkbox value to angular's ng-click
提问by Serge
Is there a way to pass to angular directive ng-click the value of the associated input?
有没有办法传递给角度指令 ng-click 关联输入的值?
In other words, what should replace this.value
in the following:
换句话说,应该替换this.value
以下内容:
<input type="checkbox" id="cb1" ng-click="checkAll(this.value)" />
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.
附注。我不想要替代值的解决方法,我只是想知道是否可以将输入的值作为参数传递给 angular function。
回答by Nidhish Krishnan
You can do the following things:
您可以执行以下操作:
- Use ng-modelif 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"
- 如果要将 html 组件绑定到 angular 范围,请使用ng-model
- 更改
ng-click
为ng-change
- 如果您必须在选中和取消选中复选框时绑定一些值,请使用
ng-true-value="someValue"
和ng-false-value="someValue"
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.
ng-click
和的执行顺序ng-model
不明确,因为它们没有定义明确的优先级。相反,你应该使用NG-变化或$watch
上$scope
,以确保您获得的模型变量的正确值。
Courtesy of musically_ut
<input type="checkbox" id="cb1" ng-model="check" ng-change="checkAll(check)" ng-true-value="YES" ng-false-value="NO"/> Citizen
回答by Arntor
Today i wanted to do the same thing and i see nobody answered the actual question. This is against the Angular philosophy, but you can replace "this.value" with "$event.target.checked" :
今天我想做同样的事情,但我看到没有人回答实际问题。这违反了 Angular 哲学,但您可以将 "this.value" 替换为 "$event.target.checked" :
<input type="checkbox" id="cb1" ng-click="checkAll($event.target.checked)" />
回答by Sajal
Assigning an ng-model to it will return boolean value depending upon the change to it:
为它分配一个 ng-model 将根据对它的更改返回布尔值:
<input type="checkbox" id="cb1" ng-model="checkValue" ng-change="checkAll(checkValue)" />
Better to use ng-change rather than ng-click
最好使用 ng-change 而不是 ng-click
回答by Daniel Tran
Bind the checkbox to a value and pass it to ng-click
.
将复选框绑定到一个值并将其传递给ng-click
.
<input type="checkbox" ng-model="value" id="cb1" ng-click="checkAll(value)" />