简体   繁体   中英

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. 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. I am looking for the simplest solution possible. Any thoughts?

Add ng-model in your checkbox.

And for checkBox angular has 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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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