How to check if any Checkbox is checked in Angular

后端 未结 6 1673
伪装坚强ぢ
伪装坚强ぢ 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 08:06

    This is re-post for insert code also. This example included: - One object list - Each object hast child list. Ex:

    var list1 = {
        name: "Role A",
        name_selected: false,
        subs: [{
          sub: "Read",
          id: 1,
          selected: false
        }, {
          sub: "Write",
          id: 2,
          selected: false
        }, {
          sub: "Update",
          id: 3,
          selected: false
        }],
      };
    

    I'll 3 list like above and i'll add to a one object list

    newArr.push(list1);
      newArr.push(list2);
      newArr.push(list3);
    

    Then i'll do how to show checkbox with multiple group:

    $scope.toggleAll = function(item) {
        var toogleStatus = !item.name_selected;
        console.log(toogleStatus);
        angular.forEach(item, function() {
          angular.forEach(item.subs, function(sub) {
            sub.selected = toogleStatus;
          });
        });
      };
    
      $scope.optionToggled = function(item, subs) {
        item.name_selected = subs.every(function(itm) {
          return itm.selected;
        })
        $scope.txtRet = item.name_selected;
      }
    

    HTML:

    • {{item.name}}
    • {{sub.sub}}
    {{txtRet}}
  • Fiddle: example

提交回复
热议问题