在AngularJS中单击按钮时将复选框设置为默认值

在AngularJS中单击按钮时将复选框设置为默认值,angularjs,Angularjs,我使用的是AngularJs,其中有一个复选框集合,并将其中一个项设置为选中项。index.cshtml包含以下代码: <div ng-controller="testController" ng-init="init()"> <form name="mainForm" id="createForm" ng-submit="mainForm.$valid && add()" novalidate=""> <div class=

我使用的是AngularJs,其中有一个复选框集合,并将其中一个项设置为选中项。index.cshtml包含以下代码:

  <div ng-controller="testController" ng-init="init()">
    <form name="mainForm" id="createForm" ng-submit="mainForm.$valid && add()" novalidate="">
      <div class="container" ng-show="createMenu">
        <div class="row">
          <div class="col-lg-2 col-md-2 col-sm-2 ">
            <label>Delivery Method</label>
          </div>
          <div class="col-lg-2 col-md-2 col-sm-2" ng-repeat="method in deliveryMethods">
            <input type="checkbox" id="{{method.id}}" value="{{method.value}}" name="deliveryMethod[]" ng-model="method.selected" ng-click="toggleSelection(method.value)" ng-required="!someSelected"> {{method.value}}

          </div>

        </div>
        <span style="color:red" ng-show="submitted == true &&  !someSelected">Delivery is required</span>

        <input type="button" id="btnReset" value="Cancel" ng-click="reset()" />
      </div>
    </form>
  </div>
此处默认情况下选择了“测试”。如果我选择选项test two,然后单击cancel,我想将复选框test设置为selected。我已尝试以下代码,但不起作用:

$scope.clear = function() {
  $scope.deliverySelection = ["test-up"];
  $scope.submitted = false;
  $scope.clearAll();
}

$scope.reset = function() {
  $scope.clear();
}
$scope.toggleSelection = function toggleSelection(deliveryMethods) {
  var idx = $scope.deliverySelection.indexOf(deliveryMethods);
  // is currently selected
  if (idx > -1) {
    $scope.deliverySelection.splice(idx, 1);
  } else {
    $scope.deliverySelection.push(deliveryMethods);
  }
  someSelected();
};
$scope.someSelected = true;

function someSelected() {
  $scope.someSelected = false;
  for (var i = 0; i < $scope.deliveryMethods.length; i++) {
    if ($scope.deliveryMethods[i].selected == true) {
      $scope.someSelected = true;
      return false;
    }
  }
}


$scope.clearAll = function() {
  angular.forEach($scope.deliveryMethods, function(checkbox_id) {
    if (checkbox_id.id != 1)
      checkbox_id.selected = false;
  });

}
单击“取消”按钮时,如何将复选框测试设置为选中?
谢谢

请尝试此功能,而不要使用“全部清除”功能

$scope.clearAll = function() {
  angular.forEach($scope.deliveryMethods, function(checkbox_id) {
    checkbox_id.selected = false;
    if (checkbox_id.id == 1) {
      checkbox_id.selected = true;
    }
  });
}
$scope.clearAll = function() {
  angular.forEach($scope.deliveryMethods, function(checkbox_id) {
    checkbox_id.selected = false;
    if (checkbox_id.id == 1) {
      checkbox_id.selected = true;
    }
  });
}