简体   繁体   English

如果选中复选框则执行函数 - Angular

[英]Execute function if checkbox is checked - Angular

In my app I have the following checkbox在我的应用程序中,我有以下复选框

<input class="js-rooms" 
       id="checkid" 
       ng-keypress="$event.keyCode == 13 && getItem(0)" 
       ng-click="getItem(0)" 
       type="checkbox"/>

When the input is clicked and checked then the function getItem(0) is called.单击并检查输入时,将调用函数getItem(0) However, when I click the checkbox again and uncheck it and re-submit then the same function gets fired, obviously.但是,当我再次单击复选框并取消选中它并重新提交时,显然会触发相同的功能。

What I would like to do(in the simplest way possible) is to fire off the function getItem(1) when the checkbox is clicked and fire off getItem(0) when the checkbox is NOT clicked.我想要做的(以最简单的方式)是在单击复选框时触发函数getItem(1)在未单击复选框时触发getItem(0) I am looking for the simplest solution possible.我正在寻找最简单的解决方案。 Any thoughts?有什么想法吗?

Add ng-model in your checkbox.在复选框中添加ng-model

And for checkBox angular has ng-true-value && ng-false-value对于 checkBox angular 有ng-true-value && ng-false-value

Like this '像这样 '

<input class="js-rooms" 
       ng-model="checkid" 
       ng-keypress="$event.keyCode == 13 && setRoom(checkid)" 
       ng-click="getItem(checkid)"  
       ng-true-value="1"
       ng-false-value="0" 
       type="checkbox"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM