Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS checkbox avoid counting when uncheck checkBox

Tags:

angularjs

Here is my running code in Plunker

I got a bug: When I uncheck the answer, I do not want to count how many times we select this answer? We will only count the number when we check the answer. For example, I check "Yes", I uncheck "Yes", I check "Yes" again, I will only show "Yes" was selected 2 times, instead of 3 times.

I read some posts about this issue, but cannot make it work by using checked property?

<input type="checkbox" ng-change="answerSelected(ans)" ng-model="checkAnswer">{{ans.answer}}
like image 661
FullStackDeveloper Avatar asked Jul 05 '17 04:07

FullStackDeveloper


1 Answers

change the ng-model to object property like ng-model="ans.checkAnswer"

<input type="checkbox" ng-change="answerSelected(ans)" ng-model="ans.checkAnswer">{{ans.answer}}

Then change the function like this

$scope.answerSelected = function(checkAnswer) {
    if (checkAnswer.checkAnswer) {
        checkAnswer.count++;
    }
    $scope.answerBoxSelected = true;
};

Demo

like image 118
Sachila Ranawaka Avatar answered Nov 11 '22 12:11

Sachila Ranawaka