Antonio Vasilev
Antonio Vasilev

Reputation: 2965

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?

Upvotes: 2

Views: 1396

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

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"/>

Upvotes: 2

Related Questions