Angular根据值禁用ng-repeat复选框

时间:2016-04-22 05:54:35

标签: javascript jquery angularjs checkbox

我有一个使用ng-repeat显示的复选框列表,现在我需要禁用一些基于特定值的复选框复选框。

checkbox display code

<div class="checkbox">
     <label ng-repeat="specific in specifications">
          <input ng-click="onClick(specific, newObject[specific])" id="specifications_chk" value="{{specific}}" ng-model="newObject[specific]" type="checkbox">
          {{specific}}
     </label>
</div>

这里使用ng-click功能禁用复选框。

This is my controller code:

$scope.onClick = function(sp, checked){
        if(sp == 'Sofa_Cleaning' && checked === true){
            angular.element(document.getElementById('specifications_chk'))[0].disabled = true;
        }

        if(sp == 'Sofa_Cleaning' && checked === false){
            angular.element(document.getElementById('specifications_chk'))[0].disabled = false;
        }

    };

html view:
控制器代码中的

我只能禁用第一个值(IndepthHomeCleaning),那么当Room_Cleaning, Kitchen_Cleaning,Carpet_cleaning时,如何禁用Sofa_Cleaningchecked checkbox view

我坚持这个,帮助将非常感激

7 个答案:

答案 0 :(得分:1)

  

我不知道为什么你仍然坚持使用jquery。你来了   当你试图禁用复选框时按id。

  

在html中,id对于当前的html页面应该是唯一的。你是   有重复的ID,你从第一个值获得   当你试图禁用时使用[o]的数组。

  

如果您严格要求使用相同方法的解决方案,请使用   以下方法使用类名而不是id作为复选框并使用   角元素和禁用。

在需要禁用的地方应使用以下代码。根据您的需要使用适当的代码。

    angular.forEach(document.getElementsByClassName('checkbox1'), function(value, key) {
        angular.element(value)[0].disabled = true;
        debugger
  });

答案 1 :(得分:0)

使用ng-disabled

&#13;
&#13;
var angApp = angular.module('myApp', []);

angApp.controller('MyController', function MyController($scope) {
  $scope.specifications = [{"text":"IndepthHomeCleaning ", "disabled":false, "checked":false},
                           {"text":"Room_Cleaning", "disabled":false, "checked":false},
                           {"text":"Kitchen_Cleaning", "disabled":false, "checked":false},
                           {"text":"BathroomCleaning", "disabled":false, "checked":false},
                           {"text":"Carpet_cleaning", "disabled":false, "checked":false},
                           {"text":"Sofa_Cleaning", "disabled":false, "checked":false}];
  
  $scope.onClick = function(sp, checked){
    console.log(sp, checked);
        if(sp.text == 'Sofa_Cleaning' && checked){
            $scope.specifications[1].disabled = true;
            $scope.specifications[2].disabled = true;
            $scope.specifications[4].disabled = true;
        }

        if(sp.text == 'Sofa_Cleaning' && !checked){
           $scope.specifications[1].disabled = false;
           $scope.specifications[2].disabled = false;
           $scope.specifications[4].disabled = false;
        }

    };
  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyController">
  <div class="checkbox">
     <label ng-repeat="specific in specifications" style="display:block">
          <input ng-disabled="specific.disabled" ng-change="onClick(specific, specific.checked)" id="specifications_chk" value="specific.checked}" ng-model="specific.checked" type="checkbox">
          {{specific.text}}
     </label>
</div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我认为你已经过度复杂了。您的数据对象应简化为如下所示

$scope.specifications = [
    {name: 'Sofa_Cleaning', active: false},
    {name: 'Carpet_Cleaning', active: false},
    {name: 'Room_Cleaning', active: false},
    {name: 'Kitchen_Cleaning', active: false},
    {name: 'BathroomCleaning', active: false}
];

模板:

<input type="checkbox"
    ng-bind="specific.name" 
    ng-model="specific.active"
    ng-disabled="shouldDisable(specific)">
控制器中的

$scope.disableWhenSofaIsChecked = ['Room_Cleaning','Kitchen_Cleaning','Carpet_Cleaning'];
$scope.shouldDisable = function(specific) {
    var disable = $scope.disableWhenSofaIsChecked;
    if(disable.indexOf(specific.name) > -1) {
        for(var obj in $scope.specifications) {
            if(obj.name === 'Sofa_Cleaning') {
                return obj.active;
            }
        }
    }
    return false;
}

修改

因此,如果您从API获取规范数据,则可以执行以下操作:

SomeApi
    .get()
    .then(function(results){
        // Assuming it's a promise. End result is the same for a passed-in callback
        // Assuming results is an array of names 
        $scope.specifications = results.map(function(item){
            return {
                name: item,
                active: false
            };
        });
    });

答案 3 :(得分:0)

您的代码中的多个元素不应具有相同的ID。你需要让元素ID独一无二。

HTML:

<div ng-app ng-controller="LoginController">
  <div class="checkbox">
    <label ng-repeat="specific in specifications">
      <input id="specifications_chk{{$index}}" ng-click="onClick(specific, newObject[specific])" value="{{specific}}" ng-model="newObject[specific]" type="checkbox" /> {{specific}}
      <br>
    </label>
  </div>
</div>

JavaScript:

function LoginController($scope) {
  $scope.specifications = ['IndepthHomeCleaning',
    'Room_Cleaning',
    'Kitchen_Cleaning',
    'BathroomCleaning',
    'Carpet_cleaning',
    'Sofa_Cleaning'
  ];
  $scope.newObject = {
    'IndepthHomeCleaning': 1,
    'Room_Cleaning': 2,
    'Kitchen_Cleaning': 3,
    'BathroomCleaning': 4,
    'Carpet_cleaning': 5,
    'Sofa_Cleaning': 6
  };
  $scope.onClick = function(sp, checked) {
    if (sp == 'Sofa_Cleaning' && checked === true) {
      angular.element(document.getElementById('specifications_chk1'))[0].disabled = true;
      angular.element(document.getElementById('specifications_chk2'))[0].disabled = true;
      angular.element(document.getElementById('specifications_chk4'))[0].disabled = true;
    }

    if (sp == 'Sofa_Cleaning' && checked === false) {
      angular.element(document.getElementById('specifications_chk1'))[0].disabled = false;
      angular.element(document.getElementById('specifications_chk2'))[0].disabled = false;
      angular.element(document.getElementById('specifications_chk3'))[0].disabled = false;
    }


  };
}

JsFiddle:https://jsfiddle.net/nikdtu/f7k3kcwn/

答案 4 :(得分:0)

您正在使用&#39; id&#39;属性对于每个DOM元素应该是唯一的。所以你的解决方案肯定会采用单一元素。所以你应该尝试使用class属性和document.getElementbyClass函数。

答案 5 :(得分:0)

如果要更改specifications数组,则需要新的数组和映射。 为每个规范定义新的行对象。 从您的规范中访问规范行及其prop(已激活或已禁用)。

http://jsfiddle.net/ms403Ly8/87/

function testCtrl($scope, $filter) {
$scope.specifications = ["IndepthHomeCleaning", "Room_Cleaning", "Kitchen_Cleaning", "Carpet_cleaning", "Sofa_Cleaning"];
$scope.specificationRows = [];

$scope.newRow = function(spec) {
    var newSpec = {
        SpecName: spec,
        IsDisabled: false,
        IsClicked: false
    };
    $scope.specificationRows.push(newSpec);
};

$scope.getRowObject = function(sp) {
    return $filter('filter')($scope.specificationRows, {
        SpecName: sp
    })[0];
};

$scope.getRowStatus = function(spec) {
    console.log($filter('filter')($scope.specificationRows, {
        SpecName: spec
    })[0].IsDisabled);
    return $scope.getRowObject(spec).IsDisabled;
};

$scope.onClick = function(sp) {
    $scope.getRowObject(sp).IsClicked = !$scope.getRowObject(sp).IsClicked;
    if (sp == 'Sofa_Cleaning') {
        $scope.getRowObject("Carpet_cleaning").IsDisabled = $scope.getRowObject(sp).IsClicked;
        $scope.getRowObject("Kitchen_Cleaning").IsDisabled = $scope.getRowObject(sp).IsClicked;
        $scope.getRowObject("Room_Cleaning").IsDisabled = $scope.getRowObject(sp).IsClicked;
    }
};

}

getRowObject就是一个例子。也许这不是最好的做法。您可以更改此功能。

答案 6 :(得分:-1)

不要使用jquery进行dom操作。

使用ng-change而不是ng-click。这将是合适的。

这里我更新了一个plunkr。

这里有一个简单的例子,我做的就像 如果值为check5则禁用所有复选框。否则启用复选框。

https://jsfiddle.net/AvGKj/517/

注意:我使用了一点旧插件。请单独使用逻辑

<div ng-controller="MyCtrl">
    <table>
    <tr ng-repeat="appObj in myAppObjects">
        <td>
        <input type="checkbox" ng-model="appObj.cb1"
        ng-disabled="appObj.disable"
        ng-change="Disable(appObj,myAppObjects)">
        {{appObj.value}}
            </td>
    </tr>
    </table>
    {{myAppObjects}}
    <br>list of checked items: {{checkedItems()}}
</div>

function MyCtrl($scope) {

        $scope.myAppObjects = [
       {id:1,value:'check1'},
       {id:2,value:'check2'},
       {id:3,value:'check3'},
       {id:4,value:'check4'},
       {id:5,value:'check5'}
       ];

      $scope.Disable=function(appObj,list)
      {
        if(appObj.value=="check5")
        {
             for(var i=0; i<list.length;i++)
           {
           if(list[i].value!="check5")
              {
                if(appObj.cb1==true)
                {
                list[i].disable=true;
                }
                else{
                list[i].disable=false;
                }
              }
           }
        }
      }
}