How to check if any Checkbox is checked in Angular

后端 未结 6 1676
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 07:39

Is there any function or ng-something to check if any of the displayed Checkboxes are checked?

I have the values through the ng-click=\"function()\"

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 07:47

    Try to think in terms of a model and what happens to that model when a checkbox is checked.

    Assuming that each checkbox is bound to a field on the model with ng-model then the property on the model is changed whenever a checkbox is clicked:

    
    
    

    and in the controller:

    $scope.fooSelected = false;
    $scope.baaSelected = false;
    

    The next button should only be available under certain circumstances so add the ng-disabled directive to the button:

    
    

    Now the next button should only be available when either fooSelected is true or baaSelected is true and we need to watch any changes to these fields to make sure that the next button is made available or not:

    $scope.$watch('[fooSelected,baaSelected]', function(){
        $scope.nextButtonDisabled = !$scope.fooSelected && !scope.baaSelected;
    }, true );
    

    The above assumes that there are only a few checkboxes that affect the availability of the next button but it could be easily changed to work with a larger number of checkboxes and use $watchCollection to check for changes.

提交回复
热议问题