簡體   English   中英

復選框標簽ID在ngrepeat中遞增。

[英]Checkbox label id increment within ngrepeat.

<div class="checkbox"><input type="checkbox" id="1"><label for="1"></label></div>

我想設置復選框的樣式,因此必須有一個ID和一個標簽,復選框才能正常工作(選中或取消選中)。

我可以通過增量js使用純js進行設置,但這不是有角度的js方法。 我將其放在ngrepeat中,將其視為待辦事項列表,如何使其遞增?

您可以使用ng-repeat的索引部分,即密鑰:

<div class="checkbox" ng-repeat="(index,value) in items">
   <input type="checkbox" id="{{index}}"><label for="{{index}}"></label>
</div>

或使用子范圍$index

<div class="checkbox" ng-repeat="item in items">
   <input type="checkbox" id="{{$index}}"><label for="{{$index}}"></label>
</div>

http://plnkr.co/edit/Yh02rAuH40XU0hB4eQO6?p=preview

兩種方法之間的區別在於,您假定范圍$index僅足以使項目為序數。 key方法中,您實際上可以訪問items數組上的原始索引(但您也可以在$scope.item訪問它)

您可以使用$ index在數組中查找項目的索引( 小提琴示例

<input type="checkbox" id="{{$index}}"/>
<label for="{{$index}}">{{$index}}</label>

暫無
暫無

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

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