簡體   English   中英

在ngrepeat angularjs上獲取選中的復選框

[英]Getting the checked checkbox on ngrepeat angularjs

我在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'正常工作。

使用[]代替.

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

Plnkr演示

腳本

$scope.priceinformation ={};

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

  $scope.priceinformation.subscriptioninfo ={};
<div ng-repeat="item in checks">
    <input type="checkbox" ng-model="item.checked">{{item.name}}
</div>

見Plunkr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM