【问题标题】:Set all checkboxes to checked as default with Checklist-model使用 Checklist-model 将所有复选框设置为默认选中
【发布时间】:2017-10-23 17:09:45
【问题描述】:

我正在使用Checklist-model 显示一些带有复选框的数据。数据是作为承诺的结果提供的。该列表正在填充,但我想将所有复选框设置为默认选中。我怎样才能做到这一点?

这是我的代码:

datas.getRules().then(
  function (resRules) 
  {
    $scope.rules = resRules.data._embedded.rules;
    $scope.versionForm.ignoreRule = $scope.rules.map(r => r.id);

    console.log($scope.versionForm.ignoreRule);
  },
  function (resRulesErr) 
  {
     console.log(resRulesErr);
  }
);
<table class="table">
  <thead>
    <tr>
      <th>Include</th>
      <th>Rule</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="r in rules track by $index">
      <td><input type="checkbox" checklist-model="versionForm.ignoreRule" checklist-value="r.id" />
      </td>
      <td>{{r.expression}}</td>
    </tr>
  </tbody>
</table>

$scope.versionForm.ignoreRule 打印到控制台时,会显示 [64, 67, 18]。

【问题讨论】:

标签: angularjs checklist-model


【解决方案1】:

您的代码有几个问题:

$scope.versionForm.ignoreRule = $scope.rules.map(r => r.id);

应该是

$scope.versionForm.ignoreRule = $scope.rules;

您想使用一组规则(对象),但实际上其中有一个 id 数组。

<tr ng-repeat="r in rules track by $index">

应该是

<tr ng-repeat="r in rules track by r.id">

而且您必须确保 id 是唯一的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2013-02-04
    相关资源
    最近更新 更多