【问题标题】:Getting the checked checkbox on ngrepeat angularjs在 ng repeat angularjs 中获取选中的复选框
【发布时间】:2015-05-20 08:56:48
【问题描述】:

我在 angularjs 中使用 ng-repeat 来显示几个复选框。

<div class="checkbox" ng-repeat="subject in priceinformation.subjects">
   <label>
       <input type="checkbox" ng-model="priceinformation.subscriptioninfo.subject.name"> {{subject.name}}
   </label>
</div>

问题是所有复选框都使用相同的模型,因此如果我选中一个,则所有复选框都被选中。我想以模型可以解析为不同值的方式来做到这一点。例如

ng-model="priceinformation.subscriptioninfo.subject.{{subject.name}}"

{{subject.name}} 应该解析为英语、历史等内容,因此每个复选框都有不同的模型。 但是ng-model="priceinformation.subscriptioninfo.subject.{{subject.name}}" 给出了一个错误。

注意:{{subject.name}} 在其他地方使用正确解析,并且“priceinformation.subscriptioninfo.subject”工作正常。

【问题讨论】:

  • 请格式化您的问题和/或包含一个 jsfiddle

标签: angularjs


【解决方案1】:

使用[] 而不是.

ng-model="priceinformation.subscriptioninfo[subject.name]"

Plnkr Demo

脚本

$scope.priceinformation ={};

  $scope.priceinformation.subjects = [{"name":"English"},{"name":"History"},{"name":"Maths"}];

  $scope.priceinformation.subscriptioninfo ={};

【讨论】:

    【解决方案2】:
    <div ng-repeat="item in checks">
        <input type="checkbox" ng-model="item.checked">{{item.name}}
    </div>
    

    See Plunkr

    【讨论】:

      最近更新 更多