Detect if checkbox is checked or unchecked in Angular.js ng-change event

前端 未结 2 1576
南笙
南笙 2021-01-31 08:04

I want to detect if a checkbox has been checked or unchecked when a click is happening on the checkbox.

This is what I have:



        
相关标签:
2条回答
  • 2021-01-31 08:29

    The state of the checkbox will be reflected on whatever model you have it bound to, in this case, $scope.answers[item.questID]

    0 讨论(0)
  • 2021-01-31 08:41

    You could just use the bound ng-model (answers[item.questID]) value itself in your ng-change method to detect if it has been checked or not.

    Example:-

    <input type="checkbox" ng-model="answers[item.questID]" 
         ng-change="stateChanged(item.questID)" /> <!-- Pass the specific id -->
    

    and

    $scope.stateChanged = function (qId) {
       if($scope.answers[qId]){ //If it is checked
           alert('test');
       }
    }
    
    0 讨论(0)
提交回复
热议问题