Search code examples
javascriptangularjsvalidationangularjs-ng-disabled

Setting validation for button if atleast one check box is selected in ng-repeat


I'm facing a problem in which if I need to enable the save button if at least one check box is selected which is inside ng-repeat. When I click for the first time it works well but dosen't work for multiple check box clicks.

Below is the working plunker:

Disabling save button

I'm using ng-change to get the selected condition..

 $scope.getSelectedState = () => {
                var selectedCount = new Array();

                for (var i in $scope.selected) {
                    selectedCount.push($scope.selected[i]);
                }
                var allTrue = selectedCount.every(function (k) { return k });
                if (allTrue) {
                    $scope.isSelected = false;
                } else {
                    $scope.isSelected = true;
                }
            }

Solution

  • just change your getSelectedState . see PLUNKER DEMO

    like:

     $scope.getSelectedState = function() {
         $scope.isSelected = true;
         angular.forEach($scope.selected, function(key, val) {   
         if(key) {
           $scope.isSelected = false;
         }
       });
     };
    

    and you should use ng-repeat in <tr> tag instead of <body> tag according to your plunker demo.