2017-08-12 106 views
1

我想用ng-repeat生成複選框。從下拉列表中選擇一個值後,我將根據選定的值獲取值。但是,在選擇任何複選框後,只有第一個複選框被選中。我無法選擇其他複選框。任何人都可以告訴如何選擇任何一個複選框或多個複選框,以便我可以基於ng-model綁定這些值。從ng-repeat動態生成複選框

下面是HTML代碼

<div class="form-group" data-ng-repeat="collegesByType in 
    collegesByTypes"> 
    <div class="be-checkbox"> 
    <input data-ng-model="committee.collegeId" name="collegeId" 
    type="hidden" 
    value="0"> 
    <input data-to-date="currently_todate386" 
    id="currently_checkbox386" class="currently" type="checkbox" 
    value="1"> 
    <label for="currently_checkbox386">{{collegesByType.name}}</label> 
    </div> 
    </div> 
+1

你應該定義單獨的模型爲每個複選框。如'data-ng-model =「collegesByType .committee.collegeId」' –

回答

0

嘗試像這樣:

<div class="form-group" data-ng-repeat="collegesByType in 
    collegesByTypes track by $index"> 
    <div class="be-checkbox"> 
    <input data-ng-model="committee.collegeId[collegesByType.collegeId]" name="collegeId" 
    type="hidden" 
    ng-value="collegesByType.collegeId"> 
    <input data-to-date="currently_todate386" 
    id="currently_checkbox386{{$index}}" class="currently" type="checkbox" 
    value="1"> 
    <label for="currently_checkbox386{{$index}}">{{collegesByType.name}}</label> 
    </div> 
</div> 
+0

是的,現在我可以選擇單個複選框。謝謝。我可以通過data-ng-model =「獲取所選複選框的值以進行保存」 committee.collegeId [$指數]「? –

+0

你會得到$ scope.committee.collegeId對象內所有選中的複選框 –

+0

它是因爲我只傳遞委託對象來保存,因爲它有其他fields.I綁定其他fields與委員會對象和collegeId是其中一個字段 –