如果条件存在,需要取消选中列表中的复选框

时间:2016-06-07 11:47:41

标签: angularjs

我之前曾问过一个相关的问题:

Checkbox and ng-change. Need to uncheck if condition exists

这对于一个复选框很好,但现在我有一个列表:

<tr ng-repeat="part in partstoorder">
    <td>

<input type="checkbox"
    ng-change="change(ordered, $index, part)"
    ng-true-value="1"
    ng-false-value="0"
    ng-model="ordered"
    ng-checked="checkedStatus"
    name="part-ordered"  />
    </td>
</tr>

JS:

$scope.change = function(value, part_index, part) {

    var part_id = part.id,
        vehicle_id = part.vehicle_id,
        ordered_from_val = part.ordered_from;


    if (modal_hidden_event) {
        $scope.checkedStatus = false;
    }

}

我如何只取消选中当前复选框?

2 个答案:

答案 0 :(得分:1)

试试这个:

<tr ng-repeat="part in partstoorder">
  <td>
    <input type="checkbox"
      ng-change="change(ordered, $index, part)"
      ng-true-value="1"
      ng-false-value="0"
      ng-model="part.ordered"
      ng-checked="part.checkedStatus"
      name="part-ordered"  />
  </td>
</tr>

此处部分是一个变量,可用于访问ng-repeat中的项目。

答案 1 :(得分:0)

只需使用数组

<tr ng-repeat="part in partstoorder">
<td>
<input type="checkbox"
    ng-change="change(ordered, $index, part)"
    ng-true-value="1"
    ng-false-value="0"
    ng-model="ordered"
    ng-checked="checkedStatus[$index] = true"
    name="part-ordered"  />
    </td>
</tr>

JS

 $scope.checkedStatus = [];
 $scope.change = function(value, part_index, part) {
   var part_id = part.id,
   vehicle_id = part.vehicle_id,
   ordered_from_val = part.ordered_from;
   if (modal_hidden_event) {
     $scope.checkedStatus[part_index] = false;
   }
 }